KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > memory > renderings > FormatTableRenderingDialog


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.views.memory.renderings;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.debug.internal.ui.DebugUIMessages;
16 import org.eclipse.debug.internal.ui.DebugUIPlugin;
17 import org.eclipse.debug.internal.ui.memory.IPersistableDebugElement;
18 import org.eclipse.debug.internal.ui.preferences.IDebugPreferenceConstants;
19 import org.eclipse.debug.ui.DebugUITools;
20 import org.eclipse.debug.ui.IDebugUIConstants;
21 import org.eclipse.jface.dialogs.IDialogConstants;
22 import org.eclipse.jface.dialogs.TrayDialog;
23 import org.eclipse.jface.preference.IPreferenceStore;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.events.SelectionListener;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Combo;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Group;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.swt.widgets.Table;
37 import org.eclipse.swt.widgets.TableColumn;
38 import org.eclipse.swt.widgets.TableItem;
39 import org.eclipse.swt.widgets.Text;
40 import org.eclipse.ui.PlatformUI;
41 import org.eclipse.ui.part.PageBook;
42
43 public class FormatTableRenderingDialog extends TrayDialog
44     {
45
46         private int[] fColumnSizes = new int[] {1, 2, 4, 8, 16};
47         private int[] fRowSizes = new int[] {1, 2, 4, 8, 16};
48         private Combo fColumnControl;
49         private Combo fRowControl;
50         
51         private int fCurrentColIdx = -1;
52         private int fCurrentRowIdx = -1;
53         private Control fPreivewPage;
54         private PageBook fPreviewPageBook;
55         private Button fDefaultButton;
56         private Text fDefaultColValue;
57         private Text fDefaultRowValue;
58         private AbstractBaseTableRendering fRendering;
59         private int fColumnSize;
60         private int fRowSize;
61         private Label fMsgLabel;
62         private boolean fDisableCancel = false;
63         private String JavaDoc fMsg;
64         
65
66         public FormatTableRenderingDialog(AbstractBaseTableRendering rendering, Shell parentShell) {
67             super(parentShell);
68             setShellStyle(getShellStyle() | SWT.RESIZE);
69             fRendering = rendering;
70             fMsg = DebugUIMessages.FormatTableRenderingAction_1;
71         }
72         
73         protected Control createDialogArea(Composite parent) {
74             getShell().setText(DebugUIMessages.FormatTableRenderingAction_0);
75             PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IDebugUIConstants.PLUGIN_ID + ".FormatTableRenderingDialog_context"); //$NON-NLS-1$
76

77             Composite composite = new Composite(parent, SWT.NONE);
78             GridLayout layout = new GridLayout();
79             layout.numColumns = 3;
80             layout.makeColumnsEqualWidth = false;
81             composite.setLayout(layout);
82             GridData data = new GridData();
83             data.grabExcessHorizontalSpace = true;
84             data.grabExcessVerticalSpace = true;
85             data.horizontalAlignment = SWT.FILL;
86             data.verticalAlignment = SWT.FILL;
87             composite.setLayoutData(data);
88             
89             fMsgLabel = new Label(composite, SWT.NONE);
90             fMsgLabel.setText(fMsg);
91             
92             data = new GridData();
93             data.grabExcessHorizontalSpace = true;
94             data.horizontalAlignment = SWT.BEGINNING;
95             data.horizontalSpan = 3;
96             fMsgLabel.setLayoutData(data);
97             
98             Label rowLabel = new Label(composite, SWT.NONE);
99             rowLabel.setText(DebugUIMessages.FormatTableRenderingAction_2);
100             fRowControl = new Combo(composite, SWT.READ_ONLY);
101             for (int i=0; i<fRowSizes.length; i++)
102             {
103                 fRowControl.add(String.valueOf(fRowSizes[i]));
104             }
105             
106             fRowControl.addSelectionListener(new SelectionListener() {
107
108                 public void widgetSelected(SelectionEvent e) {
109                     if (fCurrentRowIdx != fRowControl.getSelectionIndex())
110                     {
111                         fCurrentRowIdx = fRowControl.getSelectionIndex();
112                         refreshPreviewPage();
113                         updateButtons();
114                     }
115                 }
116
117                 public void widgetDefaultSelected(SelectionEvent e) {
118                 }});
119             
120             data = new GridData();
121             data.grabExcessHorizontalSpace = false;
122             data.horizontalAlignment = SWT.BEGINNING;
123             fRowControl.setLayoutData(data);
124             
125             Label unit = new Label(composite, SWT.NONE);
126             unit.setText(DebugUIMessages.FormatTableRenderingAction_3);
127             
128             Label columnLabel = new Label(composite, SWT.NONE);
129             columnLabel.setText(DebugUIMessages.FormatTableRenderingAction_4);
130             fColumnControl = new Combo(composite, SWT.READ_ONLY);
131             for (int i=0; i<fColumnSizes.length; i++)
132             {
133                 fColumnControl.add(String.valueOf(fColumnSizes[i]));
134             }
135             
136             fColumnControl.addSelectionListener(new SelectionListener() {
137
138                 public void widgetSelected(SelectionEvent e) {
139                     if (fCurrentColIdx != fColumnControl.getSelectionIndex())
140                     {
141                         fCurrentColIdx = fColumnControl.getSelectionIndex();
142                         refreshPreviewPage();
143                         updateButtons();
144                     }
145                 }
146
147                 public void widgetDefaultSelected(SelectionEvent e) {
148                 }});
149             
150             data = new GridData();
151             data.grabExcessHorizontalSpace = false;
152             data.horizontalAlignment = SWT.BEGINNING;
153             fColumnControl.setLayoutData(data);
154             
155             unit = new Label(composite, SWT.NONE);
156             unit.setText(DebugUIMessages.FormatTableRenderingAction_5);
157             
158             populateDialog();
159             
160             fDefaultButton = new Button(composite, SWT.NONE);
161             fDefaultButton.setText(DebugUIMessages.FormatTableRenderingAction_8);
162             data = new GridData();
163             data.grabExcessHorizontalSpace = true;
164             data.grabExcessVerticalSpace = true;
165             data.horizontalAlignment = SWT.END;
166             data.horizontalSpan = 3;
167             fDefaultButton.setLayoutData(data);
168             
169             fDefaultButton.addSelectionListener(new SelectionListener() {
170
171                 public void widgetSelected(SelectionEvent e) {
172                     saveDefaults();
173                 }
174
175                 public void widgetDefaultSelected(SelectionEvent e) {
176                     
177                 }});
178             
179             Group group = new Group(composite, SWT.NONE);
180             group.setText(DebugUIMessages.FormatTableRenderingAction_7);
181             group.setLayout(new GridLayout());
182             data = new GridData();
183             data.grabExcessHorizontalSpace = true;
184             data.grabExcessVerticalSpace = true;
185             data.horizontalAlignment = SWT.FILL;
186             data.verticalAlignment = SWT.FILL;
187             data.horizontalSpan = 3;
188             group.setLayoutData(data);
189             
190             fPreviewPageBook = new PageBook(group, SWT.NONE);
191             data = new GridData();
192             data.grabExcessHorizontalSpace = true;
193             data.grabExcessVerticalSpace = true;
194             data.horizontalAlignment = SWT.FILL;
195             data.verticalAlignment = SWT.FILL;
196             fPreviewPageBook.setLayoutData(data);
197             
198             int rowSize = fRowSizes[fRowControl.getSelectionIndex()];
199             int colSize = fColumnSizes[fColumnControl.getSelectionIndex()];
200
201             fPreivewPage = createPreviewPage(fPreviewPageBook, rowSize, colSize);
202             fPreviewPageBook.showPage(fPreivewPage);
203             
204             Label defaultRow = new Label(composite, SWT.NONE);
205             defaultRow.setText(DebugUIMessages.FormatTableRenderingDialog_0);
206             fDefaultRowValue = new Text(composite, SWT.READ_ONLY);
207             
208             int defRow = getDefaultRowSize();
209                         
210             fDefaultRowValue.setText(String.valueOf(defRow));
211             Label def = new Label(composite, SWT.NONE);
212             def.setText(DebugUIMessages.FormatTableRenderingDialog_1);
213             
214             data = new GridData();
215             data.grabExcessHorizontalSpace = true;
216             data.grabExcessVerticalSpace = true;
217             data.horizontalAlignment = SWT.BEGINNING;
218             def.setLayoutData(data);
219             
220             
221             Label defaultCol = new Label(composite, SWT.NONE);
222             defaultCol.setText(DebugUIMessages.FormatTableRenderingDialog_2);
223             fDefaultColValue = new Text(composite, SWT.READ_ONLY);
224             
225             int defCol = getDefaultColumnSize();
226             
227             fDefaultColValue.setText(String.valueOf(defCol));
228             def = new Label(composite, SWT.NONE);
229             def.setText(DebugUIMessages.FormatTableRenderingDialog_3);
230             
231             data = new GridData();
232             data.grabExcessHorizontalSpace = true;
233             data.grabExcessVerticalSpace = true;
234             data.horizontalAlignment = SWT.BEGINNING;
235             def.setLayoutData(data);
236             
237             Button restoreButton = new Button(composite, SWT.NONE);
238             restoreButton.setText(DebugUIMessages.FormatTableRenderingAction_6);
239             restoreButton.addSelectionListener(new SelectionListener() {
240                 public void widgetSelected(SelectionEvent e) {
241                     restoreDefaults();
242                 }
243                 public void widgetDefaultSelected(SelectionEvent e) {
244                 }});
245             data = new GridData();
246             data.grabExcessHorizontalSpace = true;
247             data.horizontalAlignment = SWT.END;
248             data.horizontalSpan = 3;
249             data.verticalAlignment = SWT.CENTER;
250             restoreButton.setLayoutData(data);
251             
252             return composite;
253         }
254
255         protected void okPressed() {
256             int idx = fColumnControl.getSelectionIndex();
257             fColumnSize = fColumnSizes[idx];
258             fRowSize = fRowSizes[fRowControl.getSelectionIndex()];
259             super.okPressed();
260         }
261         
262         private void populateDialog()
263         {
264             int currentColSize = fRendering.getAddressableUnitPerColumn();
265             int currentRowSize = fRendering.getAddressableUnitPerLine();
266             setCurrentRowColSizes(currentRowSize, currentColSize);
267         }
268
269         private int populateControl(int currentSize, int[] searchArray, Combo control) {
270             int idx = 0;
271             for (int i=0 ;i<searchArray.length; i++)
272             {
273                 if (searchArray[i] == currentSize)
274                 {
275                     idx = i;
276                     break;
277                 }
278             }
279             control.select(idx);
280             return idx;
281         }
282         
283         private Control createPreviewPage(Composite parent, int rowSize, int colSize)
284         {
285             if (!isValid(rowSize, colSize))
286             {
287                 Label label = new Label(parent, SWT.NONE);
288                 StringBuffer JavaDoc errorMsg = new StringBuffer JavaDoc();
289                 errorMsg.append(DebugUIMessages.FormatTableRenderingAction_9);
290                 errorMsg.append("\n"); //$NON-NLS-1$
291
errorMsg.append(DebugUIMessages.FormatTableRenderingAction_11);
292                 
293                 if (colSize > rowSize)
294                 {
295                     errorMsg.append("\n"); //$NON-NLS-1$
296
errorMsg.append(DebugUIMessages.FormatTableRenderingAction_13);
297                 }
298                 
299                 label.setText(errorMsg.toString());
300                 
301                 return label;
302             }
303             
304             Table table = new Table(parent, SWT.BORDER);
305             table.setHeaderVisible(true);
306             
307             int numCol = rowSize/colSize;
308             
309             TableColumn addressCol = new TableColumn(table, SWT.NONE);
310             
311             TableColumn[] columns = new TableColumn[numCol];
312             for (int i=0; i<columns.length; i++)
313             {
314                 columns[i] = new TableColumn(table, SWT.NONE);
315             }
316             
317             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
318             for (int j=0; j<colSize; j++)
319             {
320                 buf.append("X"); //$NON-NLS-1$
321
}
322             
323             for (int i = 0; i < 4; i++) {
324                 TableItem tableItem = new TableItem(table, SWT.NONE);
325                 
326                 String JavaDoc[] text = new String JavaDoc[numCol + 1];
327                 text[0] = DebugUIMessages.FormatTableRenderingAction_15;
328                 for (int j=1; j<text.length; j++)
329                 {
330                     text[j] = buf.toString();
331                 }
332                 
333                 tableItem.setText(text);
334             }
335             
336             addressCol.pack();
337             for (int i=0; i<columns.length; i++)
338             {
339                 columns[i].pack();
340             }
341             
342             
343             return table;
344         }
345         
346         private boolean isValid(int rowSize, int colSize)
347         {
348             if (rowSize % colSize != 0)
349                 return false;
350             
351             if (colSize > rowSize)
352                 return false;
353             
354             return true;
355         }
356
357         private void refreshPreviewPage() {
358             fPreivewPage.dispose();
359             
360             int rowSize = fRowSizes[fRowControl.getSelectionIndex()];
361             int colSize = fColumnSizes[fColumnControl.getSelectionIndex()];
362             fPreivewPage = createPreviewPage(fPreviewPageBook, rowSize, colSize);
363             fPreviewPageBook.showPage(fPreivewPage);
364         }
365
366         private void updateButtons() {
367             int rowSize = fRowSizes[fRowControl.getSelectionIndex()];
368             int colSize = fColumnSizes[fColumnControl.getSelectionIndex()];
369             Button button = getButton(IDialogConstants.OK_ID);
370             if (!isValid(rowSize, colSize))
371             {
372                 button.setEnabled(false);
373                 fDefaultButton.setEnabled(false);
374                 
375                 
376             }
377             else
378             {
379                 button.setEnabled(true);
380                 fDefaultButton.setEnabled(true);
381             }
382         }
383
384         private String JavaDoc getRowPrefId(String JavaDoc modelId) {
385             String JavaDoc rowPrefId = IDebugPreferenceConstants.PREF_ROW_SIZE + ":" + modelId; //$NON-NLS-1$
386
return rowPrefId;
387         }
388
389         private String JavaDoc getColumnPrefId(String JavaDoc modelId) {
390             String JavaDoc colPrefId = IDebugPreferenceConstants.PREF_COLUMN_SIZE + ":" + modelId; //$NON-NLS-1$
391
return colPrefId;
392         }
393         
394         private int getDefaultRowSize()
395         {
396             int size = -1;
397             IPersistableDebugElement elmt = (IPersistableDebugElement)fRendering.getMemoryBlock().getAdapter(IPersistableDebugElement.class);
398             if (elmt != null)
399             {
400                 if (elmt.supportsProperty(fRendering, IDebugPreferenceConstants.PREF_ROW_SIZE_BY_MODEL))
401                     return getDefaultFromPersistableElement(IDebugPreferenceConstants.PREF_ROW_SIZE_BY_MODEL);
402             }
403             
404             size = getDefaultRowSize(fRendering.getMemoryBlock().getModelIdentifier());
405             
406             return size;
407         }
408         
409         private int getDefaultColumnSize()
410         {
411             int size = -1;
412             IPersistableDebugElement elmt = (IPersistableDebugElement)fRendering.getMemoryBlock().getAdapter(IPersistableDebugElement.class);
413             if (elmt != null)
414             {
415                 if (elmt.supportsProperty(fRendering, IDebugPreferenceConstants.PREF_COL_SIZE_BY_MODEL))
416                     return getDefaultFromPersistableElement(IDebugPreferenceConstants.PREF_COL_SIZE_BY_MODEL);
417             }
418
419             size = getDefaultColumnSize(fRendering.getMemoryBlock().getModelIdentifier());
420             return size;
421         }
422         
423         private int getDefaultRowSize(String JavaDoc modelId)
424         {
425             int row = DebugUITools.getPreferenceStore().getInt(getRowPrefId(modelId));
426             if (row == 0)
427             {
428                 DebugUITools.getPreferenceStore().setValue(getRowPrefId(modelId), IDebugPreferenceConstants.PREF_ROW_SIZE_DEFAULT);
429                 row = DebugUITools.getPreferenceStore().getInt(getRowPrefId(modelId));
430             }
431             
432             return row;
433             
434         }
435         
436         private int getDefaultColumnSize(String JavaDoc modelId)
437         {
438             int col = DebugUITools.getPreferenceStore().getInt(getColumnPrefId(modelId));
439             if (col == 0)
440             {
441                 // if not yet defined, initialize with default
442
DebugUITools.getPreferenceStore().setValue(getColumnPrefId(modelId), IDebugPreferenceConstants.PREF_COLUMN_SIZE_DEFAULT);
443                 col = DebugUITools.getPreferenceStore().getInt(getColumnPrefId(modelId));
444             }
445             return col;
446         }
447
448         private void saveDefaults() {
449             int columnSize = fColumnSizes[fColumnControl.getSelectionIndex()];
450             int rowSize = fRowSizes[fRowControl.getSelectionIndex()];
451             
452             IPersistableDebugElement elmt = (IPersistableDebugElement)fRendering.getMemoryBlock().getAdapter(IPersistableDebugElement.class);
453             
454             if (elmt != null && elmt.supportsProperty(fRendering, IDebugPreferenceConstants.PREF_ROW_SIZE_BY_MODEL)
455                 && elmt.supportsProperty(fRendering, IDebugPreferenceConstants.PREF_COL_SIZE_BY_MODEL))
456             {
457                 try {
458                     elmt.setProperty(fRendering, IDebugPreferenceConstants.PREF_ROW_SIZE_BY_MODEL, new Integer JavaDoc(rowSize));
459                     elmt.setProperty(fRendering, IDebugPreferenceConstants.PREF_COL_SIZE_BY_MODEL, new Integer JavaDoc(columnSize));
460                 } catch (CoreException e) {
461                     DebugUIPlugin.errorDialog(DebugUIPlugin.getShell(), DebugUIMessages.FormatTableRenderingDialog_4, DebugUIMessages.FormatTableRenderingDialog_5, e);
462                 }
463             }
464             else
465             {
466                 // save preference
467
// find model id
468
String JavaDoc modelId = fRendering.getMemoryBlock().getModelIdentifier();
469                 
470                 // constrcut preference id
471
String JavaDoc rowPrefId = getRowPrefId(modelId);
472                 String JavaDoc colPrefId = getColumnPrefId(modelId);
473                 
474                 // save setting
475
IPreferenceStore prefStore = DebugUITools.getPreferenceStore();
476                 prefStore.setValue(rowPrefId, rowSize);
477                 prefStore.setValue(colPrefId, columnSize);
478             }
479             
480             fDefaultColValue.setText(String.valueOf(getDefaultColumnSize()));
481             fDefaultRowValue.setText(String.valueOf(getDefaultRowSize()));
482             
483             fDefaultRowValue.getParent().layout();
484         }
485
486         private void restoreDefaults() {
487             String JavaDoc modelId = fRendering.getMemoryBlock().getModelIdentifier();
488             int defaultRowSize = DebugUITools.getPreferenceStore().getInt(getRowPrefId(modelId));
489             int defaultColSize = DebugUITools.getPreferenceStore().getInt(getColumnPrefId(modelId));
490             
491             if (defaultRowSize == 0 || defaultColSize == 0)
492             {
493                 defaultRowSize = DebugUITools.getPreferenceStore().getInt(IDebugPreferenceConstants.PREF_ROW_SIZE);
494                 defaultColSize = DebugUITools.getPreferenceStore().getInt(IDebugPreferenceConstants.PREF_COLUMN_SIZE);
495                 
496                 DebugUITools.getPreferenceStore().setValue(getRowPrefId(modelId), defaultRowSize);
497                 DebugUITools.getPreferenceStore().setValue(getColumnPrefId(modelId), defaultColSize);
498             }
499             
500             populateControl(defaultRowSize, fRowSizes, fRowControl);
501             populateControl(defaultColSize, fColumnSizes, fColumnControl);
502             
503             fCurrentRowIdx = fRowControl.getSelectionIndex();
504             fCurrentColIdx = fColumnControl.getSelectionIndex();
505             
506             refreshPreviewPage();
507             updateButtons();
508         }
509
510         public int getColumnSize() {
511             return fColumnSize;
512         }
513
514         public int getRowSize() {
515             return fRowSize;
516         }
517         
518         public void setCurrentRowColSizes(int row, int col)
519         {
520             fCurrentColIdx = populateControl(col, fColumnSizes, fColumnControl);
521             fCurrentRowIdx = populateControl(row, fRowSizes, fRowControl);
522         }
523
524         protected Control createButtonBar(Composite parent) {
525             Control ret = super.createButtonBar(parent);
526             if (fDisableCancel)
527                 getButton(IDialogConstants.CANCEL_ID).setEnabled(false);
528             updateButtons();
529             return ret;
530         }
531         
532         public void openError(String JavaDoc msg)
533         {
534             fDisableCancel = true;
535             fMsg = msg;
536             open();
537         }
538         
539         private int getDefaultFromPersistableElement(String JavaDoc propertyId) {
540             int defaultValue = -1;
541             IPersistableDebugElement elmt = (IPersistableDebugElement)fRendering.getMemoryBlock().getAdapter(IPersistableDebugElement.class);
542             if (elmt != null)
543             {
544                 try {
545                     Object JavaDoc valueMB = elmt.getProperty(this, propertyId);
546                     if (valueMB != null && !(valueMB instanceof Integer JavaDoc))
547                     {
548                         IStatus status = DebugUIPlugin.newErrorStatus("Model returned invalid type on " + propertyId, null); //$NON-NLS-1$
549
DebugUIPlugin.log(status);
550                     }
551                     
552                     if (valueMB != null)
553                     {
554                         Integer JavaDoc value = (Integer JavaDoc)valueMB;
555                         defaultValue = value.intValue();
556                     }
557                 } catch (CoreException e) {
558                     DebugUIPlugin.log(e);
559                 }
560             }
561             return defaultValue;
562         }
563     }
564
Popular Tags