001: /**
002: *
003: */package newprocess.diagram.cust.annotations.dialogs;
004:
005: import java.util.Arrays;
006: import java.util.List;
007:
008: import org.eclipse.gef.EditPart;
009: import org.eclipse.jface.dialogs.Dialog;
010: import org.eclipse.jface.viewers.CellEditor;
011: import org.eclipse.jface.viewers.IStructuredContentProvider;
012: import org.eclipse.jface.viewers.IStructuredSelection;
013: import org.eclipse.jface.viewers.TableViewer;
014: import org.eclipse.jface.viewers.TextCellEditor;
015: import org.eclipse.jface.viewers.Viewer;
016: import org.eclipse.jface.window.IShellProvider;
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.events.FocusAdapter;
019: import org.eclipse.swt.events.FocusEvent;
020: import org.eclipse.swt.events.MouseAdapter;
021: import org.eclipse.swt.events.MouseEvent;
022: import org.eclipse.swt.events.SelectionAdapter;
023: import org.eclipse.swt.events.SelectionEvent;
024: import org.eclipse.swt.events.VerifyEvent;
025: import org.eclipse.swt.events.VerifyListener;
026: import org.eclipse.swt.layout.GridData;
027: import org.eclipse.swt.layout.GridLayout;
028: import org.eclipse.swt.widgets.Button;
029: import org.eclipse.swt.widgets.Composite;
030: import org.eclipse.swt.widgets.Control;
031: import org.eclipse.swt.widgets.Shell;
032: import org.eclipse.swt.widgets.Table;
033: import org.eclipse.swt.widgets.TableColumn;
034: import org.eclipse.swt.widgets.Text;
035:
036: /**
037: * @author sh
038: *
039: */
040: public class AnnotationDialog extends Dialog implements
041: IAnnotationViewer {
042: private EditPart selectedEditPart = null;
043: private String projectName = null;
044: private Table table = null;
045: private TableViewer tableViewer = null;
046: private Button closeButton = null;
047:
048: // set the dialgs utility class
049: private AnnotationDialogUtil dialogUtil = null;
050:
051: // Create a ExampleAnnotationList
052: private AnnotationList annotationList = null;
053:
054: // Set the table column property names
055: private final String URL_COLUMN = "url";
056:
057: // Set column names
058: private String[] columnNames = new String[] { URL_COLUMN };
059:
060: public AnnotationDialog(IShellProvider parentShell) {
061: super (parentShell);
062: }
063:
064: public AnnotationDialog(Shell parentShell,
065: EditPart selectedEditPart, String projectName) {
066: super (parentShell);
067: setShellStyle(getShellStyle() | SWT.RESIZE);
068: this .selectedEditPart = selectedEditPart;
069: dialogUtil = new AnnotationDialogUtil();
070: this .projectName = projectName;
071: }
072:
073: @Override
074: protected void configureShell(Shell newShell) {
075: super .configureShell(newShell);
076: newShell.setText("Annotations");
077: }
078:
079: @Override
080: protected Control createContents(Composite parent) {
081: // create the top level composite for the dialog
082:
083: Composite composite = new Composite(parent, 0);
084: GridLayout layout = new GridLayout();
085: layout.marginHeight = 0;
086: layout.marginWidth = 0;
087: layout.verticalSpacing = 0;
088: composite.setLayout(layout);
089: composite.setLayoutData(new GridData(
090: GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_BOTH));
091:
092: applyDialogFont(composite);
093: // initialize the dialog units
094: initializeDialogUnits(composite);
095: // create the dialog area and button bar
096: dialogArea = createDialogArea(composite);
097: return composite;
098: }
099:
100: /**
101: * create a composite to hold the children
102: */
103: @Override
104: protected Control createDialogArea(Composite parent) {
105: Composite composite = (Composite) super
106: .createDialogArea(parent);
107:
108: // Create a composite to hold the children
109: GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
110: | GridData.FILL_BOTH);
111: composite.setLayoutData(gridData);
112:
113: // Set numColumns to 5 for the buttons
114: GridLayout layout = new GridLayout(5, false);
115: layout.marginWidth = 4;
116: composite.setLayout(layout);
117:
118: // Create the table
119: createTable(composite);
120:
121: // Create and setup the TableViewer
122: createTableViewer();
123: tableViewer.setContentProvider(new AnnotationContentProvider());
124: tableViewer.setLabelProvider(new AnnotationLabelProvider());
125: // The input for the table viewer is the instance of ExampleTaskList
126: annotationList = new AnnotationList(selectedEditPart,
127: projectName);
128: annotationList.addChangeListener(this );
129: tableViewer.setInput(annotationList);
130:
131: // Add the buttons
132: createButtons(composite);
133:
134: return composite;
135: }
136:
137: /**
138: * Create the Table
139: */
140: private void createTable(Composite parent) {
141: int style = SWT.SINGLE | SWT.BORDER /*| SWT.H_SCROLL*/
142: | SWT.V_SCROLL | SWT.FULL_SELECTION
143: | SWT.HIDE_SELECTION;
144:
145: //final int NUMBER_COLUMNS = 1;
146:
147: table = new Table(parent, style);
148:
149: GridData gridData = new GridData(GridData.FILL_BOTH);
150: gridData.grabExcessVerticalSpace = true;
151: gridData.horizontalSpan = 4;
152: gridData.verticalSpan = 2;
153: gridData.heightHint = 120;
154:
155: table.setLayoutData(gridData);
156: table.setLinesVisible(true);
157: table.setHeaderVisible(true);
158:
159: // 1st column with url
160: TableColumn column = new TableColumn(table, SWT.CENTER, 0);
161: column.setText("URL");
162: column.setWidth(400);
163: // Add listener to column so urls are sorted by the url when clicked
164: column.addSelectionListener(new SelectionAdapter() {
165: public void widgetSelected(SelectionEvent e) {
166: //maybe we can set a column sorter here
167: //tableViewer.setSorter(new ExampleTaskSorter(ExampleTaskSorter.PERCENT_COMPLETE));
168: }
169: });
170: }
171:
172: /**
173: * Add the "Up", "Down", "Add", "Delete", "Link", "Open" and "Close" buttons
174: * @param parent the parent composite
175: */
176: private void createButtons(Composite parent) {
177: // Create and configure the "Up" button
178: Button up = new Button(parent, SWT.PUSH | SWT.CENTER);
179: up.setText("Up");
180: GridData gridData = new GridData(
181: GridData.HORIZONTAL_ALIGN_BEGINNING
182: | GridData.VERTICAL_ALIGN_BEGINNING);
183: gridData.widthHint = 50;
184: up.setLayoutData(gridData);
185: up.addSelectionListener(new SelectionAdapter() {
186: // Remove the selection and refresh the view
187: public void widgetSelected(SelectionEvent e) {
188: Annotation annotation = (Annotation) ((IStructuredSelection) tableViewer
189: .getSelection()).getFirstElement();
190: if (annotation != null) {
191: annotationList.moveAnnotationUp(annotation);
192: }
193: }
194: });
195:
196: // Create and configure the "Down" button
197: Button down = new Button(parent, SWT.PUSH | SWT.CENTER);
198: down.setText("Down");
199: gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
200: | GridData.VERTICAL_ALIGN_BEGINNING);
201: gridData.widthHint = 50;
202: down.setLayoutData(gridData);
203: down.addSelectionListener(new SelectionAdapter() {
204: // Remove the selection and refresh the view
205: public void widgetSelected(SelectionEvent e) {
206: Annotation annotation = (Annotation) ((IStructuredSelection) tableViewer
207: .getSelection()).getFirstElement();
208: if (annotation != null) {
209: annotationList.moveAnnotationDown(annotation);
210: }
211: }
212: });
213:
214: // Create and configure the "Add" button
215: Button add = new Button(parent, SWT.PUSH | SWT.CENTER);
216: add.setText("Add");
217: gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
218: gridData.widthHint = 80;
219: add.setLayoutData(gridData);
220: add.addSelectionListener(new SelectionAdapter() {
221: // Add an annotation to the ExampleAnnotationList and refresh the view
222: public void widgetSelected(SelectionEvent e) {
223: annotationList.addAnnotation();
224: }
225: });
226:
227: // Create and configure the "Delete" button
228: Button delete = new Button(parent, SWT.PUSH | SWT.CENTER);
229: delete.setText("Delete");
230: gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
231: gridData.widthHint = 80;
232: delete.setLayoutData(gridData);
233: delete.addSelectionListener(new SelectionAdapter() {
234: // Remove the selection and refresh the view
235: public void widgetSelected(SelectionEvent e) {
236: Annotation annotation = (Annotation) ((IStructuredSelection) tableViewer
237: .getSelection()).getFirstElement();
238: if (annotation != null) {
239: annotationList.removeAnnotation(annotation);
240: }
241: }
242: });
243:
244: // Create and configure the "Link to" button
245: Button link = new Button(parent, SWT.PUSH | SWT.CENTER);
246: link.setText("Link");
247: gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
248: gridData.widthHint = 80;
249: link.setLayoutData(gridData);
250: link.addSelectionListener(new SelectionAdapter() {
251: // Remove the selection and refresh the view
252: public void widgetSelected(SelectionEvent e) {
253: annotationList.linkToAnnotation();
254: }
255: });
256:
257: // Create and configure the "Open" button
258: Button open = new Button(parent, SWT.PUSH | SWT.CENTER);
259: open.setText("Open");
260: gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
261: gridData.widthHint = 80;
262: open.setLayoutData(gridData);
263: open.addSelectionListener(new SelectionAdapter() {
264: // Remove the selection and refresh the view
265: public void widgetSelected(SelectionEvent e) {
266: // open the selected annotation in the editor
267: Annotation annotation = (Annotation) ((IStructuredSelection) tableViewer
268: .getSelection()).getFirstElement();
269: if (annotation != null) {
270: String path = annotation.getUri();
271: AnnotationDialogUtil diaUtil = new AnnotationDialogUtil();
272: diaUtil.openAnnotation(path, projectName);
273:
274: // close the dialog
275: table.getParent().getParent().getParent().dispose();
276: }
277: }
278: });
279:
280: // Create and configure the "Close" button
281: closeButton = new Button(parent, SWT.PUSH | SWT.CENTER);
282: closeButton.setText("Close");
283: gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
284: gridData.widthHint = 50;
285: closeButton.setLayoutData(gridData);
286: closeButton.addSelectionListener(new SelectionAdapter() {
287: // Close the view i.e. dispose of the composite's parent
288: public void widgetSelected(SelectionEvent e) {
289: table.getParent().getParent().getParent().dispose();
290: }
291: });
292: }
293:
294: /**
295: * Return the column names in a collection
296: * @return List containing column names
297: */
298: public List<String> getColumnNames() {
299: return Arrays.asList(columnNames);
300: }
301:
302: /**
303: * Return the ExampleAnnotationList
304: */
305: public AnnotationList getAnnotationList() {
306: return annotationList;
307: }
308:
309: /**
310: * Create the TableViewer
311: */
312: private void createTableViewer() {
313: tableViewer = new TableViewer(table);
314: tableViewer.setUseHashlookup(true);
315: tableViewer.setColumnProperties(columnNames);
316:
317: // Create the cell editors
318: CellEditor[] editors = new CellEditor[columnNames.length];
319: //TextCellEditor textEditor = new TextCellEditor(table);
320:
321: // Column 1 : url (Free text)
322: final TextCellEditor textCellEditor = new TextCellEditor(table);
323:
324: ((Text) textCellEditor.getControl())
325: .addVerifyListener(new VerifyListener() {
326: public void verifyText(VerifyEvent e) {
327: //e.doit = "0123456789".indexOf(e.text) >= 0 ;
328: }
329: });
330:
331: ((Text) textCellEditor.getControl())
332: .addFocusListener(new FocusAdapter() {
333: public void focusLost(FocusEvent e) {
334: String cellText = ((Text) textCellEditor
335: .getControl()).getText();
336: if ((cellText
337: .equals(AnnotationList.STANDARD_TEXT))
338: || cellText.equals("")) {
339: // TODO
340: }
341: }
342: });
343:
344: ((Text) textCellEditor.getControl())
345: .addMouseListener(new MouseAdapter() {
346: public void mouseDoubleClick(MouseEvent e) {
347: // open the selected annotation in the editor
348: String cellText = ((Text) textCellEditor
349: .getControl()).getText();
350: dialogUtil
351: .openAnnotation(cellText, projectName);
352: // close the dialog
353: table.getParent().getParent().getParent()
354: .dispose();
355: }
356: });
357: editors[0] = textCellEditor;
358:
359: // Assign the cell editors to the viewer
360: tableViewer.setCellEditors(editors);
361:
362: // Set the cell modifier for the viewer
363: tableViewer.setCellModifier(new AnnotationCellModifier(this ));
364:
365: // Set the default sorter for the viewer
366: //tableViewer.setSorter(new ExampleTaskSorter(ExampleTaskSorter.DESCRIPTION));
367: }
368:
369: public void addAnnotation(Annotation annotation) {
370: tableViewer.refresh(true);
371: }
372:
373: public void removeAnnotation(Annotation annotationl) {
374: tableViewer.refresh(true);
375: }
376:
377: public void updateAnnotation(Annotation annotation) {
378: tableViewer.refresh(true);
379: }
380:
381: /**
382: * InnerClass that acts as a proxy for the ExampleAnnotationList
383: * providing content for the Table. It implements the IAnnotationViewer
384: * interface since it must register changeListeners with the
385: * ExampleTaskList
386: */
387: class AnnotationContentProvider implements
388: IStructuredContentProvider {
389: public void inputChanged(Viewer viewer, Object oldInput,
390: Object newInput) {
391: tableViewer.refresh(true);
392: }
393:
394: public void dispose() {
395: }
396:
397: // Return the Annotations as an array of Objects
398: public Object[] getElements(Object parent) {
399: return annotationList.getAnnotations().toArray();
400: }
401: }
402: }
|