00001
00002
00003
00004
00005
00006
00007
00008
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")
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" );
00056
00057 private SashForm graphElementBase_;
00058
00059 private Button hRangeButton_;
00060 private Button vRangeButton_;
00061 private Button seriesButton_;
00062
00063
00064 public void setEnabled(boolean b) {
00065 hRangeButton_.setEnabled(b);
00066 vRangeButton_.setEnabled(b);
00067 seriesButton_.setEnabled(b);
00068
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
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"));
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});
00151 trendGraphMgr_.updateGraph();
00152 }
00153 }
00154 }
00155 });
00156
00157 vRangeButton_ = new Button(graphControlPanel, SWT.PUSH);
00158 vRangeButton_.setText(MessageBundle.get("GraphPanel.button.vrange"));
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});
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"));
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 = "";
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 += ",";
00234 dataItems += graphName+"_"+di.object+"_"+di.node+"_"+di.attribute;
00235 if (di.index >= 0)
00236 dataItems += "_"+di.index;
00237 graphItem.setProperty(header+".object",di.object);
00238 graphItem.setProperty(header+".node",di.node);
00239 graphItem.setProperty(header+".attr",di.attribute);
00240 graphItem.setProperty(header+".index", String.valueOf(di.index));
00241 graphItem.setProperty(header+".legend", list[i].legend);
00242 graphItem.setProperty(header+".color", StringConverter.asString(list[i].color));
00243 }
00244 graphItem.setProperty(graphName+".dataItems",dataItems);
00245 updateButtons();
00246 trendGraphMgr_.updateGraph();
00247 }
00248 }
00249 }
00250 );
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
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
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
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
00385
00386 GrxGraphItem p = manager_.<GrxGraphItem>getSelectedItem(GrxGraphItem.class, null);
00387 setEnabledRangeButton(p != null);
00388 }
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461 public void setModelList(List<GrxModelItem> list){
00462 currentModels_ = list;
00463 }
00464
00465 }