GraphPanel.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00003  * All rights reserved. This program is made available under the terms of the
00004  * Eclipse Public License v1.0 which accompanies this distribution, and is
00005  * available at http://www.eclipse.org/legal/epl-v10.html
00006  * Contributors:
00007  * General Robotix Inc.
00008  * National Institute of Advanced Industrial Science and Technology (AIST) 
00009  */
00016 package com.generalrobotix.ui.view.graph;
00017 
00018 import java.util.Enumeration;
00019 import java.util.List;
00020 
00021 import org.eclipse.jface.dialogs.IDialogConstants;
00022 import org.eclipse.jface.dialogs.MessageDialog;
00023 import org.eclipse.jface.resource.StringConverter;
00024 import org.eclipse.swt.SWT;
00025 import org.eclipse.swt.custom.SashForm;
00026 import org.eclipse.swt.custom.ScrolledComposite;
00027 import org.eclipse.swt.events.PaintEvent;
00028 import org.eclipse.swt.events.PaintListener;
00029 import org.eclipse.swt.events.SelectionAdapter;
00030 import org.eclipse.swt.events.SelectionEvent;
00031 import org.eclipse.swt.graphics.Color;
00032 import org.eclipse.swt.layout.GridData;
00033 import org.eclipse.swt.layout.GridLayout;
00034 import org.eclipse.swt.layout.RowLayout;
00035 import org.eclipse.swt.widgets.Button;
00036 import org.eclipse.swt.widgets.Composite;
00037 import org.eclipse.swt.widgets.Label;
00038 import org.eclipse.swt.widgets.Scale;
00039 
00040 import com.generalrobotix.ui.GrxPluginManager;
00041 import com.generalrobotix.ui.grxui.Activator;
00042 import com.generalrobotix.ui.item.GrxGraphItem;
00043 import com.generalrobotix.ui.item.GrxModelItem;
00044 import com.generalrobotix.ui.util.MessageBundle;
00045 
00046 @SuppressWarnings("serial") //$NON-NLS-1$
00047 public class GraphPanel extends Composite{
00048     //--------------------------------------------------------------------
00049     private GraphElement[] graphElement_;
00050     public GraphElement currentGraph_;
00051     private TrendGraphModel trendGraphMgr_;
00052     private List<GrxModelItem> currentModels_ = null;
00053 
00054     private static final Color normalColor_ = Activator.getDefault().getColor("black");
00055     private static final Color focusedColor_ = Activator.getDefault().getColor( "focusedColor" ); //$NON-NLS-1$
00056 
00057     private SashForm graphElementBase_;
00058     
00059     private Button hRangeButton_;
00060     private Button vRangeButton_;
00061     private Button seriesButton_;
00062     //private Button epsButton_;
00063     
00064     public void setEnabled(boolean b) {
00065         hRangeButton_.setEnabled(b);
00066         vRangeButton_.setEnabled(b);
00067         seriesButton_.setEnabled(b);
00068         //epsButton_.setEnabled(b);
00069     }
00070 
00071     public void setEnabledRangeButton(boolean b) {
00072         hRangeButton_.setEnabled(b);
00073         vRangeButton_.setEnabled(b);
00074     }
00075     
00076     private HRangeDialog hRangeDialog_;
00077     private VRangeDialog vRangeDialog_;
00078     public SeriesDialog seriesDialog_;
00079     //private EPSDialog epsDialog_;
00080 
00081     private ScrolledComposite graphScrollPane_;
00082 
00083     private int numGraph_;
00084     private GrxPluginManager manager_;
00085     private DataItemInfo[] addedArray_;
00086      
00087      public GraphPanel(GrxPluginManager manager, TrendGraphModel trendGraphMgr, Composite comp) {
00088         super(comp, SWT.NONE);
00089         manager_ = manager;
00090         trendGraphMgr_ = trendGraphMgr;
00091       
00092         setLayout(new GridLayout(1,true));
00093         graphScrollPane_ = new ScrolledComposite(this, SWT.H_SCROLL | SWT.V_SCROLL| SWT.BORDER);
00094         graphScrollPane_.setExpandHorizontal(true);
00095         graphScrollPane_.setExpandVertical(true);
00096         GridData gridData0 = new GridData();
00097                 gridData0.horizontalAlignment = GridData.FILL;
00098                 gridData0.grabExcessHorizontalSpace = true;
00099                 gridData0.verticalAlignment = GridData.FILL;
00100                 gridData0.grabExcessVerticalSpace = true;
00101                 graphScrollPane_.setLayoutData(gridData0);
00102         Composite graphControlPanel = new Composite(this, SWT.NONE); 
00103         GridData gridData1 = new GridData();
00104                 gridData1.horizontalAlignment = GridData.FILL;
00105                 gridData1.grabExcessHorizontalSpace = true;
00106         graphControlPanel.setLayoutData(gridData1);
00107         graphControlPanel.setLayout(new RowLayout());
00108         graphElementBase_ = new SashForm(graphScrollPane_, SWT.VERTICAL);
00109         graphElementBase_.SASH_WIDTH = 6;
00110         graphScrollPane_.setContent(graphElementBase_);
00111 
00112         numGraph_ = trendGraphMgr_.getNumGraph();
00113         graphElement_ = new GraphElement[numGraph_];
00114         for (int i = 0; i < numGraph_; i ++) {
00115             graphElement_[i] =
00116                 new GraphElement(
00117                         this,
00118                         graphElementBase_,
00119                     trendGraphMgr_.getTrendGraph(i) );
00120         }
00121         graphScrollPane_.setMinSize(graphElementBase_.computeSize(SWT.DEFAULT, SWT.DEFAULT));
00122 
00123         currentGraph_ = graphElement_[0];
00124         graphElement_[0].setBorderColor(focusedColor_);
00125 
00126         hRangeButton_ = new Button(graphControlPanel,  SWT.PUSH);
00127         hRangeButton_.setText(MessageBundle.get("GraphPanel.button.hrange")); //$NON-NLS-1$
00128         hRangeDialog_ = new HRangeDialog(graphControlPanel.getShell());
00129         hRangeButton_.addSelectionListener(new SelectionAdapter(){
00130                 public void widgetSelected(SelectionEvent e) {
00131                         GrxGraphItem graphItem = manager_.<GrxGraphItem>getSelectedItem(GrxGraphItem.class, null);
00132                         if (graphItem == null)
00133                                 return;
00134                         hRangeDialog_.setMaxHRange(trendGraphMgr_.getTotalTime());
00135                         hRangeDialog_.setMinHRange(trendGraphMgr_.getStepTime() * 10);
00136                         double range = trendGraphMgr_.getTimeRange();
00137                         double pos = trendGraphMgr_.getMarkerPos();
00138                         hRangeDialog_.setHRange(range);
00139                         hRangeDialog_.setMarkerPos(pos);
00140                         if(hRangeDialog_.open() == IDialogConstants.OK_ID){
00141                                 int flag = hRangeDialog_.getUpdateFlag();
00142                     if (flag != 0) {
00143                         TrendGraph tg = currentGraph_.getTrendGraph();
00144                         double hRange = hRangeDialog_.getHRange();
00145                         double mpos =   hRangeDialog_.getMarkerPos();
00146                         if ((flag & HRangeDialog.RANGE_UPDATED) != 0
00147                             || (flag & HRangeDialog.POS_UPDATED) != 0) {
00148                             tg.setTimeRangeAndPos(hRange, mpos);
00149                         }
00150                         graphItem.setDblAry("timeRange", new double[]{hRange, mpos}); //$NON-NLS-1$
00151                         trendGraphMgr_.updateGraph();
00152                     }
00153                 }
00154                 }
00155         });
00156 
00157         vRangeButton_ = new Button(graphControlPanel,  SWT.PUSH);
00158         vRangeButton_.setText(MessageBundle.get("GraphPanel.button.vrange")); //$NON-NLS-1$
00159         vRangeDialog_ = new VRangeDialog(graphControlPanel.getShell());
00160         vRangeButton_.addSelectionListener(new SelectionAdapter(){
00161                 public void widgetSelected(SelectionEvent e) {
00162                     TrendGraph tg = currentGraph_.getTrendGraph();
00163                     vRangeDialog_.setUnit(tg.getUnitLabel());
00164                     vRangeDialog_.setBase(tg.getBase());
00165                     vRangeDialog_.setExtent(tg.getExtent());
00166                     if(vRangeDialog_.open() == IDialogConstants.OK_ID){
00167                         double base = vRangeDialog_.getBase();
00168                         double extent = vRangeDialog_.getExtent();
00169                         tg.setRange(base, extent);
00170                         GrxGraphItem graphItem = manager_.<GrxGraphItem>getSelectedItem(GrxGraphItem.class, null);
00171                         if (graphItem == null)
00172                                 return;
00173                         graphItem.setDblAry(currentGraph_.getTrendGraph().getNodeName()+".vRange", new double[]{base, extent}); //$NON-NLS-1$
00174                         redraw(getLocation().x,getLocation().y,getSize().x,getSize().y,true);
00175                     }
00176                 }
00177             }
00178         );
00179 
00180         seriesButton_ = new Button(graphControlPanel,  SWT.PUSH);
00181         seriesButton_.setText(MessageBundle.get("GraphPanel.button.series")); //$NON-NLS-1$
00182         
00183         seriesDialog_ = new SeriesDialog(currentGraph_, graphControlPanel.getShell());
00184         seriesButton_.addSelectionListener(new SelectionAdapter(){
00185                 public void widgetSelected(SelectionEvent e) {
00186                     GrxGraphItem graphItem = manager_.<GrxGraphItem>getSelectedItem(GrxGraphItem.class, null);
00187                     if (graphItem == null)
00188                     {
00189                         if(MessageDialog.openQuestion(
00190                             null,
00191                             MessageBundle.get("GraphPanel.dialog.creategraph.title"),
00192                             MessageBundle.get("GraphPanel.dialog.creategraph.message")))
00193                         {
00194                             graphItem = (GrxGraphItem)manager_.createItem(GrxGraphItem.class, null);
00195                             manager_.itemChange(graphItem, GrxPluginManager.ADD_ITEM);
00196                             manager_.setSelectedItem(graphItem, true);
00197                         }
00198                         else
00199                             return;
00200                     }
00201 
00202                     TrendGraph tg = currentGraph_.getTrendGraph();
00203                     seriesDialog_.setModelList(currentModels_);
00204                     seriesDialog_.setDataItemInfoList(tg.getDataItemInfoList());
00205                     if(seriesDialog_.open() == IDialogConstants.OK_ID){
00206                         DataItemInfo[] dii = seriesDialog_.getDataItemInfoList();
00207                         for (int i = 0; i < dii.length; i++) {
00208                             tg.setDataItemInfo(dii[i]);
00209                         }
00210                         dii = seriesDialog_.getRemovedList();
00211                         for (int i = 0; i < dii.length; i++) {
00212                             tg.removeDataItem(dii[i]);
00213                         }
00214                         
00215                         addedArray_ = seriesDialog_.getAddedList();
00216                         for (int i = 0; i < addedArray_.length; i++) {
00217                                 tg.addDataItem(addedArray_[i]);
00218                         }
00219                         
00220                         String graphName = tg.getNodeName();
00221                         Enumeration<?> enume = graphItem.propertyNames();
00222                         while (enume.hasMoreElements()) {
00223                                 String key = (String)enume.nextElement();
00224                                 if (key.startsWith(graphName))
00225                                         graphItem.remove(key);
00226                         }
00227                         String dataItems = ""; //$NON-NLS-1$
00228                         DataItemInfo[] list = tg.getDataItemInfoList();
00229                         for (int i = 0; i<list.length;i++) {
00230                                 DataItem di = list[i].dataItem;
00231                                 String header = tg._getDataItemNodeName(di);
00232                                 if (i > 0)
00233                                         dataItems += ","; //$NON-NLS-1$
00234                                 dataItems += graphName+"_"+di.object+"_"+di.node+"_"+di.attribute; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
00235                                 if (di.index >= 0)
00236                                         dataItems += "_"+di.index; //$NON-NLS-1$
00237                                 graphItem.setProperty(header+".object",di.object); //$NON-NLS-1$
00238                                 graphItem.setProperty(header+".node",di.node); //$NON-NLS-1$
00239                                 graphItem.setProperty(header+".attr",di.attribute); //$NON-NLS-1$
00240                                 graphItem.setProperty(header+".index", String.valueOf(di.index)); //$NON-NLS-1$
00241                                 graphItem.setProperty(header+".legend", list[i].legend); //$NON-NLS-1$
00242                                 graphItem.setProperty(header+".color", StringConverter.asString(list[i].color)); //$NON-NLS-1$
00243                         }
00244                         graphItem.setProperty(graphName+".dataItems",dataItems); //$NON-NLS-1$
00245                         updateButtons();
00246                         trendGraphMgr_.updateGraph();
00247                     }
00248                 }
00249             }
00250         );
00251         /*
00252         epsButton_ = new Button(graphControlPanel,  SWT.PUSH);
00253         epsButton_.setText(MessageBundle.get("graph.eps"));
00254 
00255         epsDialog_ = new EPSDialog(owner_);
00256         epsButton_.addActionListener(
00257             new ActionListener() {
00258                 public void actionPerformed(ActionEvent evt) {
00259                     epsDialog_.setColorOutput(true);
00260                     epsDialog_.setGraphOutput(true);
00261                     epsDialog_.setLegendOutput(true);
00262                     epsDialog_.setLocationRelativeTo(GraphPanel.this);
00263                     epsDialog_.setVisible(true);
00264                     if (epsDialog_.isUpdated()) {
00265                         String path = epsDialog_.getPath();
00266                         boolean cout = epsDialog_.isColorOutput();
00267                         boolean gout = epsDialog_.isGraphOutput();
00268                         boolean lout = epsDialog_.isLegendOutput();
00269                         FileWriter fw = null;
00270 
00271                         if (path.equals("")) {
00272                             new MessageDialog(
00273                                 owner_,
00274                                 MessageBundle.get("message.filenameempty"),
00275                                 MessageBundle.get("messege.inputfilename")
00276                             ).showModalDialog();
00277                             return;
00278                         }
00279 
00280                         try {
00281                             fw = new FileWriter(path);
00282                         } catch (IOException ex) {
00283                             //ex.printStackTrace();
00284                             //System.exit(0);
00285                             new MessageDialog(
00286                                 owner_,
00287                                 MessageBundle.get("messege.fileioerror"),
00288                                 MessageBundle.get("messege.fileioerror")
00289                             ).showModalDialog();
00290                             return;
00291                         }
00292                         BufferedWriter bw = new BufferedWriter(fw);
00293                         DroppableXYGraph graph =
00294                             (DroppableXYGraph)currentGraph_.getGraph();
00295                         DroppableXYGraph.LegendPanel legend =
00296                             (DroppableXYGraph.LegendPanel)graph.getLegendPanel();
00297                         Dimension gsize = graph.getSize();
00298                         //Dimension lsize = legend.getSize();
00299                         Dimension lsize = legend.getMinimalSize();
00300                         int width = 0;
00301                         int height = 0;
00302                         int lyofs = 0;
00303                         if (gout && lout) {
00304                             width = gsize.width + lsize.width - GRAPH_RIGHT_MARGIN + 10;
00305                             if (gsize.height > lsize.height) {
00306                                 height = gsize.height;
00307                                 lyofs = (gsize.height - lsize.height) / 2;
00308                             } else {
00309                                 height = lsize.height;
00310                             }
00311                         } else if (gout) {
00312                             width = gsize.width;
00313                             height = gsize.height;
00314                         } else if (lout) {
00315                             width = lsize.width;
00316                             height = lsize.height;
00317                         }
00318                         EPSGraphics eg = new EPSGraphics(bw, 0, 0, width, height, cout);
00319                         int xofs = 0;
00320                         if (gout) {
00321                             graph.setEPSMode(true);
00322                             graph.paint(eg);
00323                             graph.setEPSMode(false);
00324                             xofs += gsize.width - GRAPH_RIGHT_MARGIN + 10;
00325                         }
00326                         if (lout) {
00327                             eg.setXOffset(xofs);
00328                             eg.setYOffset(lyofs);
00329                             legend.paint(eg);
00330                         }
00331                         eg.finishOutput();
00332                     }
00333                 }
00334             }
00335         );
00336         */
00337         setEnabledRangeButton(false);
00338     }
00339 
00340     public void setFocuse(GraphElement ge){
00341         currentGraph_.setBorderColor(normalColor_);
00342                 currentGraph_ = ge;
00343                 currentGraph_.setBorderColor(focusedColor_);
00344                 redraw(getLocation().x,getLocation().y,getSize().x,getSize().y,true);
00345         seriesDialog_.setCurrentGraph(currentGraph_);
00346         updateButtons();
00347     }
00348     //--------------------------------------------------------------------
00352     public void resetFocus() {
00353         /*
00354         SwingUtilities.invokeLater(
00355             new Runnable() {
00356                 public void run() {
00357                     currentGraph_ = graphElement_[0];
00358                     for (int i = 0; i < numGraph_; i++) {
00359                         graphElement_[i].setBorderColor(normalColor_);
00360                         graphElement_[i].setLegendBackColor(normalColor_);
00361                         //graphElement_[i].getPreferredSize().height = INITIAL_GRAPH_HEIGHT;
00362                     }
00363                     graphElement_[0].setBorderColor(focusedColor_);
00364                     graphElement_[0].setLegendBackColor(focusedColor_);
00365                     //graphScrollPane_.getViewport().setViewPosition(new Point(0, 0));
00366                     //heightSlider_.setValue(INITIAL_GRAPH_HEIGHT);
00367                     graphElementBase_.setVisible(false);
00368                     graphElementBase_.setVisible(true);
00369                     updateButtons();
00370                 }
00371             }
00372         );
00373         */
00374     }
00375 
00376     //--------------------------------------------------------------------
00380     private void updateButtons() {
00381         boolean enabled = (currentGraph_.getTrendGraph().getNumDataItems() > 0);
00382         vRangeButton_.setEnabled(enabled);
00383         seriesButton_.setEnabled(true);
00384         //epsButton_.setEnabled(enabled);
00385         
00386         GrxGraphItem p = manager_.<GrxGraphItem>getSelectedItem(GrxGraphItem.class, null);
00387         setEnabledRangeButton(p != null);
00388     }
00389     
00390 
00391  /*   public void setMode(int mode) {
00392         mode_ = mode;
00393         SwingUtilities.invokeLater(
00394             new Runnable() {
00395                 public void run() {
00396                     boolean enabled =
00397                         (currentGraph_.getTrendGraph().getNumDataItems() > 0);
00398                     if (mode_ == GUIStatus.EDIT_MODE) {
00399                         hRangeButton_.setEnabled(true);
00400                         vRangeButton_.setEnabled(enabled);
00401                         seriesButton_.setEnabled(enabled);
00402                         epsButton_.setEnabled(enabled);
00403                     } else if (mode_ == GUIStatus.EXEC_MODE) {
00404                         hRangeButton_.setEnabled(false);
00405                         vRangeButton_.setEnabled(enabled);
00406                         seriesButton_.setEnabled(false);
00407                         epsButton_.setEnabled(false);
00408                     } else if (mode_ == GUIStatus.PLAYBACK_MODE) {
00409                         hRangeButton_.setEnabled(false);
00410                         vRangeButton_.setEnabled(enabled);
00411                         seriesButton_.setEnabled(false);
00412                         epsButton_.setEnabled(enabled);
00413                     }
00414                 }
00415             }
00416         );
00417     }
00418 
00419     //--------------------------------------------------------------------
00420     // Implementation of PlaybackStatusListener
00421     public void playbackStatusChanged(int status) {
00422         final int subMode = status;
00423         SwingUtilities.invokeLater(
00424             new Runnable() {
00425                 public void run() {
00426                     boolean enabled =
00427                         (currentGraph_.getTrendGraph().getNumDataItems() > 0);
00428                     switch (subMode) {
00429                     case PlaybackStatus.STOPPED:
00430                         hRangeButton_.setEnabled(true);
00431                         vRangeButton_.setEnabled(enabled);
00432                         seriesButton_.setEnabled(enabled);
00433                         epsButton_.setEnabled(enabled);
00434                         break;
00435                     case PlaybackStatus.PLAYING:
00436                         hRangeButton_.setEnabled(false);
00437                         vRangeButton_.setEnabled(enabled);
00438                         seriesButton_.setEnabled(false);
00439                         epsButton_.setEnabled(false);
00440                         break;
00441                     case PlaybackStatus.PAUSE:
00442                         hRangeButton_.setEnabled(true);
00443                         vRangeButton_.setEnabled(enabled);
00444                         seriesButton_.setEnabled(enabled);
00445                         epsButton_.setEnabled(enabled);
00446                         break;
00447                     case PlaybackStatus.RECORDING:
00448                         hRangeButton_.setEnabled(false);
00449                         vRangeButton_.setEnabled(enabled);
00450                         seriesButton_.setEnabled(false);
00451                         epsButton_.setEnabled(false);
00452                         break;
00453                     }
00454                 }
00455             }
00456         );
00457     }*/
00458 
00459     //--------------------------------------------------------------------
00460 
00461     public void setModelList(List<GrxModelItem> list){
00462         currentModels_ = list;
00463     }
00464     
00465 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:53