KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > EditLogicalStructureDialog


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 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.jdt.internal.debug.ui;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.commands.AbstractHandler;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.commands.IHandler;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.NullProgressMonitor;
24 import org.eclipse.core.runtime.OperationCanceledException;
25 import org.eclipse.jdt.core.IType;
26 import org.eclipse.jdt.core.JavaCore;
27 import org.eclipse.jdt.core.JavaModelException;
28 import org.eclipse.jdt.core.search.IJavaSearchConstants;
29 import org.eclipse.jdt.core.search.IJavaSearchScope;
30 import org.eclipse.jdt.core.search.SearchEngine;
31 import org.eclipse.jdt.core.search.SearchMatch;
32 import org.eclipse.jdt.core.search.SearchParticipant;
33 import org.eclipse.jdt.core.search.SearchPattern;
34 import org.eclipse.jdt.core.search.SearchRequestor;
35 import org.eclipse.jdt.internal.debug.core.logicalstructures.JavaLogicalStructure;
36 import org.eclipse.jdt.internal.debug.ui.contentassist.DynamicTypeContext;
37 import org.eclipse.jdt.internal.debug.ui.contentassist.JavaDebugContentAssistProcessor;
38 import org.eclipse.jdt.internal.debug.ui.contentassist.DynamicTypeContext.ITypeProvider;
39 import org.eclipse.jdt.internal.debug.ui.display.DisplayViewerConfiguration;
40 import org.eclipse.jdt.ui.IJavaElementSearchConstants;
41 import org.eclipse.jdt.ui.JavaUI;
42 import org.eclipse.jdt.ui.text.IJavaPartitions;
43 import org.eclipse.jdt.ui.text.JavaTextTools;
44 import org.eclipse.jface.dialogs.IDialogConstants;
45 import org.eclipse.jface.dialogs.StatusDialog;
46 import org.eclipse.jface.text.Document;
47 import org.eclipse.jface.text.DocumentEvent;
48 import org.eclipse.jface.text.IDocumentListener;
49 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
50 import org.eclipse.jface.text.source.ISourceViewer;
51 import org.eclipse.jface.viewers.ISelectionChangedListener;
52 import org.eclipse.jface.viewers.IStructuredContentProvider;
53 import org.eclipse.jface.viewers.IStructuredSelection;
54 import org.eclipse.jface.viewers.LabelProvider;
55 import org.eclipse.jface.viewers.SelectionChangedEvent;
56 import org.eclipse.jface.viewers.StructuredSelection;
57 import org.eclipse.jface.viewers.TableViewer;
58 import org.eclipse.jface.viewers.Viewer;
59 import org.eclipse.swt.SWT;
60 import org.eclipse.swt.layout.GridData;
61 import org.eclipse.swt.layout.GridLayout;
62 import org.eclipse.swt.widgets.Button;
63 import org.eclipse.swt.widgets.Composite;
64 import org.eclipse.swt.widgets.Control;
65 import org.eclipse.swt.widgets.Event;
66 import org.eclipse.swt.widgets.Group;
67 import org.eclipse.swt.widgets.Listener;
68 import org.eclipse.swt.widgets.Shell;
69 import org.eclipse.swt.widgets.Table;
70 import org.eclipse.swt.widgets.Text;
71 import org.eclipse.swt.widgets.Widget;
72 import org.eclipse.ui.IWorkbench;
73 import org.eclipse.ui.PlatformUI;
74 import org.eclipse.ui.dialogs.SelectionDialog;
75 import org.eclipse.ui.handlers.IHandlerActivation;
76 import org.eclipse.ui.handlers.IHandlerService;
77 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
78
79 import com.ibm.icu.text.MessageFormat;
80
81 /**
82  * A dialog that allows users to create/edit logical structures.
83  */

84 public class EditLogicalStructureDialog extends StatusDialog implements Listener, ISelectionChangedListener, IDocumentListener, ITypeProvider {
85
86     public class AttributesContentProvider implements IStructuredContentProvider {
87
88         private final List JavaDoc fVariables;
89
90         public AttributesContentProvider(String JavaDoc[][] variables) {
91             fVariables= new ArrayList JavaDoc();
92             for (int i= 0; i < variables.length; i++) {
93                 String JavaDoc[] variable= new String JavaDoc[2];
94                 variable[0]= variables[i][0];
95                 variable[1]= variables[i][1];
96                 fVariables.add(variable);
97             }
98         }
99
100         /* (non-Javadoc)
101          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
102          */

103         public void dispose() {
104         }
105
106         /* (non-Javadoc)
107          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
108          */

109         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
110         }
111
112         /* (non-Javadoc)
113          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
114          */

115         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
116             return getElements();
117         }
118         
119         /**
120          * Returns the attributes.
121          */

122         public String JavaDoc[][] getElements() {
123             return (String JavaDoc[][])fVariables.toArray(new String JavaDoc[fVariables.size()][]);
124         }
125
126         /**
127          * Adds the given attributes.
128          */

129         public void add(String JavaDoc[] newAttribute) {
130             fVariables.add(newAttribute);
131         }
132
133         /**
134          * Remove the given attributes
135          */

136         public void remove(List JavaDoc list) {
137             fVariables.removeAll(list);
138         }
139
140         /**
141          * Moves the given attributes up in the list.
142          */

143         public void up(List JavaDoc list) {
144             for (Iterator JavaDoc iter= list.iterator(); iter.hasNext();) {
145                 String JavaDoc[] variable= (String JavaDoc[]) iter.next();
146                 int index= fVariables.indexOf(variable);
147                 fVariables.remove(variable);
148                 fVariables.add(index - 1, variable);
149             }
150         }
151
152         /**
153          * Moves the given attributes down int the list.
154          */

155         public void down(List JavaDoc list) {
156             for (Iterator JavaDoc iter= list.iterator(); iter.hasNext();) {
157                 String JavaDoc[] variable= (String JavaDoc[]) iter.next();
158                 int index= fVariables.indexOf(variable);
159                 fVariables.remove(variable);
160                 fVariables.add(index + 1, variable);
161             }
162         }
163
164     }
165     
166     public class AttributesLabelProvider extends LabelProvider {
167         public String JavaDoc getText(Object JavaDoc element) {
168             return ((String JavaDoc[])element)[0];
169         }
170     }
171     
172     private final JavaLogicalStructure fLogicalStructure;
173     private Text fQualifiedTypeNameText;
174     private Text fDescriptionText;
175     private TableViewer fAttributeListViewer;
176     private Button fSubTypeButton;
177     private Button fValueButton;
178     private Button fVariablesButton;
179     private Button fAttributeUpButton;
180     private Button fAttributeDownButton;
181     private JDISourceViewer fSnippetViewer;
182     private Document fSnippetDocument;
183     private Button fBrowseTypeButton;
184     private Button fAttributeAddButton;
185     private Button fAttributeRemoveButton;
186     private Text fAttributeNameText;
187     private Composite fAttributesContainer;
188     private Group fCodeGroup;
189     private Composite fParentComposite;
190     private AttributesContentProvider fAttributesContentProvider;
191     private String JavaDoc fValueTmp;
192     private IStructuredSelection fCurrentAttributeSelection;
193     private IType fType;
194     private boolean fTypeSearched= false;
195     private DisplayViewerConfiguration fViewerConfiguration;
196     private IHandlerActivation fHandlerActivation;
197
198     public EditLogicalStructureDialog(Shell parentShell, JavaLogicalStructure logicalStructure) {
199         super(parentShell);
200         setShellStyle(getShellStyle() | SWT.MAX | SWT.RESIZE);
201         if (logicalStructure.getQualifiedTypeName().length() == 0) {
202             setTitle(DebugUIMessages.EditLogicalStructureDialog_32);
203         } else {
204             setTitle(DebugUIMessages.EditLogicalStructureDialog_31);
205         }
206         fLogicalStructure= logicalStructure;
207     }
208     
209     /* (non-Javadoc)
210      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
211      */

212     protected Control createDialogArea(Composite parent) {
213         IWorkbench workbench = PlatformUI.getWorkbench();
214         workbench.getHelpSystem().setHelp(
215                 parent,
216                 IJavaDebugHelpContextIds.EDIT_LOGICAL_STRUCTURE_DIALOG);
217         fParentComposite= parent;
218         
219         IHandler handler = new AbstractHandler() {
220             public Object JavaDoc execute(ExecutionEvent event) throws ExecutionException {
221                 if(fSnippetViewer.canDoOperation(ISourceViewer.CONTENTASSIST_PROPOSALS) && fSnippetViewer.getControl().isFocusControl()){
222                     findCorrespondingType();
223                     fSnippetViewer.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
224                 }
225                 return null;
226             }
227         };
228         
229         IHandlerService handlerService = (IHandlerService) workbench.getAdapter(IHandlerService.class);
230         fHandlerActivation = handlerService.activateHandler(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS, handler);
231         
232         Composite container = SWTFactory.createComposite(parent, parent.getFont(), 1, 1, GridData.FILL_BOTH);
233
234         Composite typeNameDescriptionContainer = SWTFactory.createComposite(container, container.getFont(), 2, 1, GridData.FILL_HORIZONTAL);
235         
236         SWTFactory.createLabel(typeNameDescriptionContainer, DebugUIMessages.EditLogicalStructureDialog_0, 2);
237         
238         fQualifiedTypeNameText = SWTFactory.createSingleText(typeNameDescriptionContainer, 1);
239         fQualifiedTypeNameText.addListener(SWT.Modify, this);
240         
241         fBrowseTypeButton = SWTFactory.createPushButton(typeNameDescriptionContainer, DebugUIMessages.EditLogicalStructureDialog_1, DebugUIMessages.EditLogicalStructureDialog_25, null);
242         
243         fBrowseTypeButton.addListener(SWT.Selection, this);
244         
245         SWTFactory.createLabel(typeNameDescriptionContainer, DebugUIMessages.EditLogicalStructureDialog_2, 2);
246         
247         fDescriptionText = SWTFactory.createSingleText(typeNameDescriptionContainer, 2);
248         fDescriptionText.addListener(SWT.Modify, this);
249         
250         fSubTypeButton = SWTFactory.createCheckButton(typeNameDescriptionContainer, DebugUIMessages.EditLogicalStructureDialog_3, null, false, 1);
251         fSubTypeButton.setToolTipText(DebugUIMessages.EditLogicalStructureDialog_26);
252
253         Group radioContainer= SWTFactory.createGroup(container, DebugUIMessages.EditLogicalStructureDialog_33, 1, 1, GridData.FILL_HORIZONTAL);
254
255         fValueButton = SWTFactory.createRadioButton(radioContainer, DebugUIMessages.EditLogicalStructureDialog_4);
256         fValueButton.addListener(SWT.Selection, this);
257         
258         fVariablesButton = SWTFactory.createRadioButton(radioContainer, DebugUIMessages.EditLogicalStructureDialog_5);
259
260         fAttributesContainer = SWTFactory.createComposite(container, container.getFont(), 2, 1, GridData.FILL_HORIZONTAL);
261         
262         boolean isValue = fLogicalStructure.getValue() != null;
263         if (!isValue) {
264             // creates the attribute list if needed
265
createAttributeListWidgets();
266         }
267         
268         fCodeGroup = SWTFactory.createGroup(container, "", 1, 1, GridData.FILL_BOTH); //$NON-NLS-1$
269
createCodeGroupWidgets(isValue);
270
271         applyDialogFont(container);
272         
273         initializeData();
274         
275         return container;
276     }
277
278     /**
279      * Create the widgets it the code snippet editor group
280      */

281     private void createCodeGroupWidgets(boolean isValue) {
282         if (isValue) {
283             fCodeGroup.setText(DebugUIMessages.EditLogicalStructureDialog_9);
284         } else {
285             fCodeGroup.setText(DebugUIMessages.EditLogicalStructureDialog_7);
286         
287             Composite attributeNameContainer = SWTFactory.createComposite(fCodeGroup, fCodeGroup.getFont(), 2, 1, GridData.FILL_HORIZONTAL);
288             ((GridLayout)attributeNameContainer.getLayout()).marginWidth = 0;
289             
290             SWTFactory.createLabel(attributeNameContainer, DebugUIMessages.EditLogicalStructureDialog_8, 1);
291             
292             fAttributeNameText = SWTFactory.createSingleText(attributeNameContainer, 1);
293             fAttributeNameText.addListener(SWT.Modify, this);
294             
295             SWTFactory.createLabel(fCodeGroup, DebugUIMessages.EditLogicalStructureDialog_9, 1);
296         }
297         
298         // snippet viewer
299
fSnippetViewer= new JDISourceViewer(fCodeGroup, null, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL );
300         fSnippetViewer.setInput(this);
301     
302         JavaTextTools tools= JDIDebugUIPlugin.getDefault().getJavaTextTools();
303         if (fSnippetDocument == null) {
304             fSnippetDocument= new Document();
305             fSnippetDocument.addDocumentListener(this);
306         }
307         tools.setupJavaDocumentPartitioner(fSnippetDocument, IJavaPartitions.JAVA_PARTITIONING);
308         if (fViewerConfiguration == null) {
309             fViewerConfiguration= new DisplayViewerConfiguration() {
310                 public IContentAssistProcessor getContentAssistantProcessor() {
311                     return new JavaDebugContentAssistProcessor(new DynamicTypeContext(EditLogicalStructureDialog.this));
312                 }
313             };
314         }
315         fSnippetViewer.configure(fViewerConfiguration);
316         fSnippetViewer.setEditable(true);
317         fSnippetViewer.setDocument(fSnippetDocument);
318         
319         Control control= fSnippetViewer.getControl();
320         GridData gd= new GridData(SWT.FILL, SWT.FILL, true, true);
321         gd.heightHint= convertHeightInCharsToPixels(isValue ? 20 : 10);
322         gd.widthHint= convertWidthInCharsToPixels(80);
323         control.setLayoutData(gd);
324     }
325
326     /**
327      * Create the widgets for the attribute list
328      */

329     private void createAttributeListWidgets() {
330         fAttributeListViewer = new TableViewer(fAttributesContainer, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
331         Table table = (Table)fAttributeListViewer.getControl();
332         GridData gd= new GridData(SWT.FILL, SWT.FILL, true, false);
333         gd.heightHint= convertHeightInCharsToPixels(5);
334         gd.widthHint= convertWidthInCharsToPixels(10);
335         table.setLayoutData(gd);
336         table.setFont(fAttributesContainer.getFont());
337         if (fAttributesContentProvider == null) {
338             fAttributesContentProvider= new AttributesContentProvider(fLogicalStructure.getVariables());
339         }
340         fAttributeListViewer.setContentProvider(fAttributesContentProvider);
341         fAttributeListViewer.setLabelProvider(new AttributesLabelProvider());
342         fAttributeListViewer.setInput(this);
343         fAttributeListViewer.addSelectionChangedListener(this);
344         
345         Composite attributeListButtonsCotnainer = SWTFactory.createComposite(fAttributesContainer, fAttributesContainer.getFont(), 1, 1, SWT.NONE);
346         
347         fAttributeAddButton = SWTFactory.createPushButton(attributeListButtonsCotnainer, DebugUIMessages.EditLogicalStructureDialog_10, DebugUIMessages.EditLogicalStructureDialog_27, null);
348         fAttributeAddButton.addListener(SWT.Selection, this);
349         
350         fAttributeRemoveButton = SWTFactory.createPushButton(attributeListButtonsCotnainer, DebugUIMessages.EditLogicalStructureDialog_11, DebugUIMessages.EditLogicalStructureDialog_28, null);
351         fAttributeRemoveButton.addListener(SWT.Selection, this);
352         
353         fAttributeUpButton = SWTFactory.createPushButton(attributeListButtonsCotnainer, DebugUIMessages.EditLogicalStructureDialog_12, DebugUIMessages.EditLogicalStructureDialog_29, null);
354         fAttributeUpButton.addListener(SWT.Selection, this);
355         
356         fAttributeDownButton = SWTFactory.createPushButton(attributeListButtonsCotnainer, DebugUIMessages.EditLogicalStructureDialog_13, DebugUIMessages.EditLogicalStructureDialog_30, null);
357         fAttributeDownButton.addListener(SWT.Selection, this);
358     }
359
360     private void initializeData() {
361         fQualifiedTypeNameText.setText(fLogicalStructure.getQualifiedTypeName());
362         fDescriptionText.setText(fLogicalStructure.getDescription());
363         fSubTypeButton.setSelection(fLogicalStructure.isSubtypes());
364         fValueTmp= fLogicalStructure.getValue();
365         if (fValueTmp == null) {
366             fValueTmp= ""; //$NON-NLS-1$
367
fVariablesButton.setSelection(true);
368             setAttributesData(false);
369         } else {
370             fValueButton.setSelection(true);
371             setAttributesData(true);
372         }
373         checkValues();
374     }
375
376     /* (non-Javadoc)
377      * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
378      */

379     public void handleEvent(Event event) {
380         Widget source= event.widget;
381         switch (event.type) {
382             case SWT.Selection:
383                 if (source == fValueButton) {
384                     toggleAttributesWidgets(fValueButton.getSelection());
385                     checkValues();
386                 } else if (source == fBrowseTypeButton) {
387                     selectType();
388                 } else if (source == fAttributeAddButton) {
389                     addAttribute();
390                 } else if (source == fAttributeRemoveButton) {
391                     removeAttribute();
392                 } else if (source == fAttributeUpButton) {
393                     attributeUp();
394                 } else if (source == fAttributeDownButton) {
395                     attributeDown();
396                 }
397                 break;
398             case SWT.Modify:
399                 if (source == fAttributeNameText) {
400                     saveNewAttributeName();
401                     checkValues();
402                 } else if (source == fQualifiedTypeNameText) {
403                     checkValues();
404                     fTypeSearched= false;
405                 } else if (source == fDescriptionText) {
406                     checkValues();
407                 }
408                 break;
409         }
410     }
411
412     // code for add attribute button
413
private void addAttribute() {
414         String JavaDoc[] newAttribute= new String JavaDoc[] {DebugUIMessages.EditLogicalStructureDialog_14, DebugUIMessages.EditLogicalStructureDialog_15}; //
415
fAttributesContentProvider.add(newAttribute);
416         fAttributeListViewer.refresh();
417         fAttributeListViewer.setSelection(new StructuredSelection((Object JavaDoc)newAttribute));
418     }
419
420     // code for remove attribute button
421
private void removeAttribute() {
422         IStructuredSelection selection= (IStructuredSelection)fAttributeListViewer.getSelection();
423         if (selection.size() > 0) {
424             List JavaDoc selectedElements= selection.toList();
425             Object JavaDoc[] elements= fAttributesContentProvider.getElements();
426             Object JavaDoc newSelectedElement= null;
427             for (int i= 0; i < elements.length; i++) {
428                 if (!selectedElements.contains(elements[i])) {
429                     newSelectedElement= elements[i];
430                 } else {
431                     break;
432                 }
433             }
434             fAttributesContentProvider.remove(selectedElements);
435             fAttributeListViewer.refresh();
436             if (newSelectedElement == null) {
437                 Object JavaDoc[] newElements= fAttributesContentProvider.getElements();
438                 if (newElements.length > 0) {
439                     fAttributeListViewer.setSelection(new StructuredSelection(newElements[0]));
440                 }
441             } else {
442                 fAttributeListViewer.setSelection(new StructuredSelection(newSelectedElement));
443             }
444         }
445     }
446
447     // code for attribute up button
448
private void attributeUp() {
449         IStructuredSelection selection= (IStructuredSelection)fAttributeListViewer.getSelection();
450         if (selection.size() > 0) {
451             fAttributesContentProvider.up(selection.toList());
452             fAttributeListViewer.refresh();
453             fAttributeListViewer.setSelection(selection);
454         }
455     }
456
457     // code for attribute down button
458
private void attributeDown() {
459         IStructuredSelection selection= (IStructuredSelection)fAttributeListViewer.getSelection();
460         if (selection.size() > 0) {
461             fAttributesContentProvider.down(selection.toList());
462             fAttributeListViewer.refresh();
463             fAttributeListViewer.setSelection(selection);
464         }
465     }
466
467     // save the new attribute name typed by the user
468
private void saveNewAttributeName() {
469         if (fCurrentAttributeSelection.size() == 1) {
470             String JavaDoc[] variable= (String JavaDoc[])fCurrentAttributeSelection.getFirstElement();
471             variable[0]= fAttributeNameText.getText();
472             fAttributeListViewer.refresh(variable);
473         }
474     }
475
476     /*
477      * Display or hide the widgets specific to a logicial structure with
478      * variables.
479      */

480     private void toggleAttributesWidgets(boolean isValue) {
481         if (!isValue) {
482             // recreate the attribute list
483
fValueTmp= fSnippetDocument.get();
484             createAttributeListWidgets();
485         } else if (isValue) {
486             // dispose the attribute list
487
saveAttributeValue();
488             Control[] children= fAttributesContainer.getChildren();
489             for (int i= 0; i < children.length; i++) {
490                 children[i].dispose();
491             }
492         }
493         
494         // dispose and recreate the code snippet editor group
495
Control[] children= fCodeGroup.getChildren();
496         for (int i = 0; i < children.length; i++) {
497             children[i].dispose();
498         }
499         fSnippetViewer.dispose();
500         createCodeGroupWidgets(isValue);
501         setAttributesData(isValue);
502         fParentComposite.layout(true, true);
503     }
504
505     /**
506      * Set the data in the attributes and code widgets
507      */

508     private void setAttributesData(boolean isValue) {
509         if (isValue) {
510             fSnippetDocument.set(fValueTmp);
511         } else {
512             Object JavaDoc[] elements= fAttributesContentProvider.getElements(null);
513             fCurrentAttributeSelection= new StructuredSelection();
514             if (elements.length > 0) {
515                 IStructuredSelection newSelection= new StructuredSelection(elements[0]);
516                 fAttributeListViewer.setSelection(newSelection);
517             } else {
518                 fAttributeListViewer.setSelection(fCurrentAttributeSelection);
519             }
520             
521         }
522     }
523
524     /**
525      * Set the data and enable/disablet the attribute widgets for the current selection.
526      */

527     private void setNameValueToSelection() {
528         if (fCurrentAttributeSelection.size() == 1) {
529             String JavaDoc[] variable= (String JavaDoc[]) fCurrentAttributeSelection.getFirstElement();
530             fAttributeNameText.setText(variable[0]);
531             fSnippetDocument.set(variable[1]);
532             fAttributeNameText.setEnabled(true);
533             fSnippetViewer.setEditable(true);
534             fAttributeNameText.setSelection(0, variable[0].length());
535             fAttributeNameText.setFocus();
536         } else {
537             fAttributeNameText.setEnabled(false);
538             fSnippetViewer.setEditable(false);
539             fAttributeNameText.setText(""); //$NON-NLS-1$
540
fSnippetDocument.set(""); //$NON-NLS-1$
541
}
542     }
543
544     // save the code to the current attribute.
545
private void saveAttributeValue() {
546         if (fCurrentAttributeSelection.size() == 1) {
547             ((String JavaDoc[])fCurrentAttributeSelection.getFirstElement())[1]= fSnippetDocument.get();
548         }
549     }
550
551     /* (non-Javadoc)
552      * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
553      */

554     public void selectionChanged(SelectionChangedEvent event) {
555         saveAttributeValue();
556         fCurrentAttributeSelection= (IStructuredSelection) event.getSelection();
557         setNameValueToSelection();
558         updateAttributeListButtons();
559     }
560     
561     /**
562      * Enable/disable the attribute buttons for the current selection.
563      */

564     private void updateAttributeListButtons() {
565         int selectionSize= fCurrentAttributeSelection.size();
566         if (selectionSize > 0) {
567             fAttributeRemoveButton.setEnabled(true);
568             Object JavaDoc[] elements= fAttributesContentProvider.getElements();
569             fAttributeUpButton.setEnabled(fCurrentAttributeSelection.getFirstElement() != elements[0]);
570             fAttributeDownButton.setEnabled(fCurrentAttributeSelection.toArray()[selectionSize - 1] != elements[elements.length - 1]);
571         } else {
572             fAttributeRemoveButton.setEnabled(false);
573             fAttributeUpButton.setEnabled(false);
574             fAttributeDownButton.setEnabled(false);
575         }
576     }
577
578     /**
579      * Check the values in the widgets.
580      */

581     public void checkValues() {
582         StatusInfo status= new StatusInfo();
583         if (fQualifiedTypeNameText.getText().trim().length() == 0) {
584             status.setError(DebugUIMessages.EditLogicalStructureDialog_16);
585         } else if (fDescriptionText.getText().trim().length() == 0) {
586             status.setError(DebugUIMessages.EditLogicalStructureDialog_17);
587         } else if (fValueButton.getSelection() && fSnippetDocument.get().length() == 0) {
588             status.setError(DebugUIMessages.EditLogicalStructureDialog_18);
589         } else if (fVariablesButton.getSelection()) {
590             Object JavaDoc[] elements= fAttributesContentProvider.getElements(null);
591             boolean oneElementSelected= fCurrentAttributeSelection.size() == 1;
592             if (elements.length == 0) {
593                 status.setError(DebugUIMessages.EditLogicalStructureDialog_19);
594             } else if (oneElementSelected && fAttributeNameText.getText().trim().length() == 0) {
595                 status.setError(DebugUIMessages.EditLogicalStructureDialog_20);
596             } else if (oneElementSelected && fSnippetDocument.get().trim().length() == 0) {
597                 status.setError(DebugUIMessages.EditLogicalStructureDialog_21);
598             } else {
599                 for (int i= 0; i < elements.length; i++) {
600                     String JavaDoc[] variable= (String JavaDoc[]) elements[i];
601                     if (variable[0].trim().length() == 0) {
602                         status.setError(DebugUIMessages.EditLogicalStructureDialog_22);
603                         break;
604                     }
605                     if (variable[1].trim().length() == 0) {
606                         if (!oneElementSelected || fCurrentAttributeSelection.getFirstElement() != variable) {
607                             status.setError(MessageFormat.format(DebugUIMessages.EditLogicalStructureDialog_23, new String JavaDoc[] {variable[0]}));
608                             break;
609                         }
610                     }
611                 }
612             }
613         }
614         if (!status.isError()) {
615             if (fType == null && fTypeSearched) {
616                 status.setWarning(DebugUIMessages.EditLogicalStructureDialog_24);
617             }
618         }
619         updateStatus(status);
620     }
621
622     /**
623      * Open the 'select type' dialog, and set the user choice into the formatter.
624      */

625     private void selectType() {
626         Shell shell= getShell();
627         SelectionDialog dialog= null;
628         try {
629             dialog= JavaUI.createTypeDialog(shell, PlatformUI.getWorkbench().getProgressService(),
630                 SearchEngine.createWorkspaceScope(), IJavaElementSearchConstants.CONSIDER_ALL_TYPES,
631                 false, fQualifiedTypeNameText.getText());
632         } catch (JavaModelException jme) {
633             String JavaDoc title= DebugUIMessages.DetailFormatterDialog_Select_type_6;
634             String JavaDoc message= DebugUIMessages.DetailFormatterDialog_Could_not_open_type_selection_dialog_for_detail_formatters_7;
635             ExceptionHandler.handle(jme, title, message);
636             return;
637         }
638     
639         dialog.setTitle(DebugUIMessages.DetailFormatterDialog_Select_type_8);
640         dialog.setMessage(DebugUIMessages.DetailFormatterDialog_Select_a_type_to_format_when_displaying_its_detail_9);
641         if (dialog.open() == IDialogConstants.CANCEL_ID) {
642             return;
643         }
644         
645         Object JavaDoc[] types= dialog.getResult();
646         if (types != null && types.length > 0) {
647             fType = (IType)types[0];
648             fQualifiedTypeNameText.setText(fType.getFullyQualifiedName());
649             fTypeSearched = true;
650         }
651     }
652     
653     /* (non-Javadoc)
654      * @see org.eclipse.jface.text.IDocumentListener#documentAboutToBeChanged(org.eclipse.jface.text.DocumentEvent)
655      */

656     public void documentAboutToBeChanged(DocumentEvent event) {
657         // nothing to do
658
}
659     
660     /* (non-Javadoc)
661      * @see org.eclipse.jface.text.IDocumentListener#documentChanged(org.eclipse.jface.text.DocumentEvent)
662      */

663     public void documentChanged(DocumentEvent event) {
664         checkValues();
665     }
666     
667     /* (non-Javadoc)
668      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
669      */

670     protected void okPressed() {
671         // save the new data in the logical structure
672
fLogicalStructure.setType(fQualifiedTypeNameText.getText().trim());
673         fLogicalStructure.setDescription(fDescriptionText.getText().trim());
674         fLogicalStructure.setSubtypes(fSubTypeButton.getSelection());
675         if (fValueButton.getSelection()) {
676             fLogicalStructure.setValue(fSnippetDocument.get());
677         } else {
678             saveAttributeValue();
679             fLogicalStructure.setValue(null);
680         }
681         if (fAttributesContentProvider != null) {
682             fLogicalStructure.setVariables(fAttributesContentProvider.getElements());
683         }
684         super.okPressed();
685     }
686
687     /**
688      * Use the Java search engine to find the type which corresponds
689      * to the given name.
690      */

691     private void findCorrespondingType() {
692         if (fTypeSearched) {
693             return;
694         }
695         fType= null;
696         fTypeSearched= true;
697         final String JavaDoc pattern= fQualifiedTypeNameText.getText().trim().replace('$', '.');
698         if (pattern == null || "".equals(pattern)) { //$NON-NLS-1$
699
return;
700         }
701         final IProgressMonitor monitor = new NullProgressMonitor();
702         final SearchRequestor collector = new SearchRequestor() {
703             private boolean fFirst= true;
704             
705             public void endReporting() {
706                 checkValues();
707             }
708
709             public void acceptSearchMatch(SearchMatch match) throws CoreException {
710                 Object JavaDoc enclosingElement = match.getElement();
711                 if (!fFirst) {
712                     return;
713                 }
714                 fFirst= false;
715                 if (enclosingElement instanceof IType) {
716                     fType= (IType) enclosingElement;
717                 }
718                 // stop after we find one
719
monitor.setCanceled(true);
720             }
721         };
722         
723         SearchEngine engine= new SearchEngine(JavaCore.getWorkingCopies(null));
724         SearchPattern searchPattern = SearchPattern.createPattern(pattern, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
725         IJavaSearchScope scope= SearchEngine.createWorkspaceScope();
726         SearchParticipant[] participants = new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()};
727         try {
728             engine.search(searchPattern, participants, scope, collector, monitor);
729         } catch (CoreException e) {
730             JDIDebugUIPlugin.log(e);
731         } catch (OperationCanceledException e){
732         }
733     }
734     
735     /**
736      * Return the type object which corresponds to the given name.
737      */

738     public IType getType() {
739         if (!fTypeSearched) {
740             findCorrespondingType();
741         }
742         return fType;
743     }
744
745     /* (non-Javadoc)
746      * @see org.eclipse.jface.dialogs.Dialog#close()
747      */

748     public boolean close() {
749         IWorkbench workbench = PlatformUI.getWorkbench();
750         IHandlerService handlerService = (IHandlerService) workbench.getAdapter(IHandlerService.class);
751         handlerService.deactivateHandler(fHandlerActivation);
752
753         fSnippetViewer.dispose();
754         return super.close();
755     }
756
757 }
758
Popular Tags