Source Code Cross Referenced for CheatSheetCategoryBasedSelectionDialog.java in  » IDE-Eclipse » ui » org » eclipse » ui » internal » cheatsheets » dialogs » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Eclipse » ui » org.eclipse.ui.internal.cheatsheets.dialogs 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Copyright (c) 2002, 2007 IBM Corporation and others.
003:         * All rights reserved. This program and the accompanying materials
004:         * are made available under the terms of the Eclipse Public License v1.0
005:         * which accompanies this distribution, and is available at
006:         * http://www.eclipse.org/legal/epl-v10.html
007:         *
008:         * Contributors:
009:         *     IBM Corporation - initial API and implementation
010:         *******************************************************************************/package org.eclipse.ui.internal.cheatsheets.dialogs;
011:
012:        import java.io.File;
013:        import java.net.MalformedURLException;
014:        import java.net.URL;
015:        import java.util.ArrayList;
016:        import java.util.List;
017:
018:        import org.eclipse.core.runtime.IPath;
019:        import org.eclipse.core.runtime.IStatus;
020:        import org.eclipse.core.runtime.Path;
021:        import org.eclipse.core.runtime.Status;
022:        import org.eclipse.jface.dialogs.Dialog;
023:        import org.eclipse.jface.dialogs.IDialogConstants;
024:        import org.eclipse.jface.dialogs.IDialogSettings;
025:        import org.eclipse.jface.dialogs.TrayDialog;
026:        import org.eclipse.jface.viewers.DoubleClickEvent;
027:        import org.eclipse.jface.viewers.IContentProvider;
028:        import org.eclipse.jface.viewers.IDoubleClickListener;
029:        import org.eclipse.jface.viewers.ISelection;
030:        import org.eclipse.jface.viewers.ISelectionChangedListener;
031:        import org.eclipse.jface.viewers.IStructuredSelection;
032:        import org.eclipse.jface.viewers.LabelProvider;
033:        import org.eclipse.jface.viewers.SelectionChangedEvent;
034:        import org.eclipse.jface.viewers.StructuredSelection;
035:        import org.eclipse.jface.viewers.TreeViewer;
036:        import org.eclipse.jface.viewers.Viewer;
037:        import org.eclipse.jface.viewers.ViewerFilter;
038:        import org.eclipse.osgi.util.NLS;
039:        import org.eclipse.swt.SWT;
040:        import org.eclipse.swt.custom.SashForm;
041:        import org.eclipse.swt.events.ModifyEvent;
042:        import org.eclipse.swt.events.ModifyListener;
043:        import org.eclipse.swt.events.SelectionAdapter;
044:        import org.eclipse.swt.events.SelectionEvent;
045:        import org.eclipse.swt.events.SelectionListener;
046:        import org.eclipse.swt.graphics.Image;
047:        import org.eclipse.swt.layout.GridData;
048:        import org.eclipse.swt.layout.GridLayout;
049:        import org.eclipse.swt.widgets.Button;
050:        import org.eclipse.swt.widgets.Combo;
051:        import org.eclipse.swt.widgets.Composite;
052:        import org.eclipse.swt.widgets.Control;
053:        import org.eclipse.swt.widgets.FileDialog;
054:        import org.eclipse.swt.widgets.Label;
055:        import org.eclipse.swt.widgets.Shell;
056:        import org.eclipse.swt.widgets.Text;
057:        import org.eclipse.ui.ISharedImages;
058:        import org.eclipse.ui.PlatformUI;
059:        import org.eclipse.ui.activities.ITriggerPoint;
060:        import org.eclipse.ui.activities.WorkbenchActivityHelper;
061:        import org.eclipse.ui.cheatsheets.OpenCheatSheetAction;
062:        import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin;
063:        import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
064:        import org.eclipse.ui.internal.cheatsheets.Messages;
065:        import org.eclipse.ui.internal.cheatsheets.data.ParserStatusUtility;
066:        import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetCollectionElement;
067:        import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetCollectionSorter;
068:        import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetElement;
069:        import org.eclipse.ui.internal.cheatsheets.state.DefaultStateManager;
070:        import org.eclipse.ui.internal.cheatsheets.views.CheatSheetView;
071:        import org.eclipse.ui.internal.cheatsheets.views.ViewUtilities;
072:        import org.eclipse.ui.model.BaseWorkbenchContentProvider;
073:        import org.eclipse.ui.model.WorkbenchAdapter;
074:
075:        /**
076:         * Dialog to allow the user to select a cheat sheet from a list.
077:         */
078:        public class CheatSheetCategoryBasedSelectionDialog extends TrayDialog //extends SelectionDialog
079:                implements  ISelectionChangedListener {
080:            private static final String CHEAT_SHEET_SELECTION_HELP_ID = "org.eclipse.ui.cheatsheets.cheatSheetSelection"; //$NON-NLS-1$
081:
082:            private IDialogSettings settings;
083:
084:            private CheatSheetCollectionElement cheatsheetCategories;
085:
086:            private CheatSheetElement currentSelection;
087:
088:            private TreeViewer treeViewer;
089:
090:            private Text desc;
091:
092:            private Button showAllButton;
093:
094:            private Button selectRegisteredRadio;
095:
096:            private Button selectFileRadio;
097:
098:            private Button selectUrlRadio;
099:
100:            private Combo selectFileCombo;
101:
102:            private Combo selectUrlCombo;
103:
104:            private ActivityViewerFilter activityViewerFilter = new ActivityViewerFilter();
105:
106:            private boolean okButtonState;
107:
108:            // id constants
109:
110:            private static final String DIALOG_SETTINGS_SECTION = "CheatSheetCategoryBasedSelectionDialog"; //$NON-NLS-1$
111:
112:            private final static String STORE_EXPANDED_CATEGORIES_ID = "CheatSheetCategoryBasedSelectionDialog.STORE_EXPANDED_CATEGORIES_ID"; //$NON-NLS-1$
113:
114:            private final static String STORE_SELECTED_CHEATSHEET_ID = "CheatSheetCategoryBasedSelectionDialog.STORE_SELECTED_CHEATSHEET_ID"; //$NON-NLS-1$
115:
116:            private final static String STORE_RADIO_SETTING = "CheatSheetCategoryBasedSelectionDialog.STORE_RADIO_SELECTION"; //$NON-NLS-1$
117:
118:            private final static String STORE_CHEATSHEET_FILENAME = "CheatSheetCategoryBasedSelectionDialog.STORE_CHEATSHEET_FILENAME"; //$NON-NLS-1$
119:
120:            private final static String STORE_CHEATSHEET_URL = "CheatSheetCategoryBasedSelectionDialog.STORE_CHEATSHEET_URL"; //$NON-NLS-1$
121:
122:            private final static String STORE_URL_MRU = "CheatSheetCategoryBasedSelectionDialog.STORE_URL_MRU"; //$NON-NLS-1$
123:            private final static String STORE_FILE_MRU = "CheatSheetCategoryBasedSelectionDialog.STORE_FILE_MRU"; //$NON-NLS-1$
124:
125:            private static final int MOST_RECENT_LENGTH = 3;
126:            private static final int RADIO_REGISTERED = 1;
127:            private static final int RADIO_FILE = 2;
128:            private static final int RADIO_URL = 3;
129:
130:            private Button browseFileButton;
131:
132:            private String title;
133:
134:            private IStatus status = Status.OK_STATUS;
135:
136:            List mostRecentFiles = new ArrayList();
137:            List mostRecentUrls = new ArrayList();
138:
139:            private static class ActivityViewerFilter extends ViewerFilter {
140:                private boolean hasEncounteredFilteredItem = false;
141:
142:                /*
143:                 * (non-Javadoc)
144:                 * 
145:                 * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer,
146:                 *      java.lang.Object, java.lang.Object)
147:                 */
148:                public boolean select(Viewer viewer, Object parentElement,
149:                        Object element) {
150:                    if (WorkbenchActivityHelper.filterItem(element)) {
151:                        setHasEncounteredFilteredItem(true);
152:                        return false;
153:                    }
154:                    return true;
155:                }
156:
157:                /**
158:                 * @return returns whether the filter has filtered an item
159:                 */
160:                public boolean getHasEncounteredFilteredItem() {
161:                    return hasEncounteredFilteredItem;
162:                }
163:
164:                /**
165:                 * @param sets
166:                 *            whether the filter has filtered an item
167:                 */
168:                public void setHasEncounteredFilteredItem(
169:                        boolean hasEncounteredFilteredItem) {
170:                    this .hasEncounteredFilteredItem = hasEncounteredFilteredItem;
171:                }
172:            }
173:
174:            private class CheatsheetLabelProvider extends LabelProvider {
175:                public String getText(Object obj) {
176:                    if (obj instanceof  WorkbenchAdapter) {
177:                        return ((WorkbenchAdapter) obj).getLabel(null);
178:                    }
179:                    return super .getText(obj);
180:                }
181:
182:                public Image getImage(Object obj) {
183:                    if (obj instanceof  CheatSheetElement) {
184:                        CheatSheetElement element = (CheatSheetElement) obj;
185:                        if (element.isComposite()) {
186:                            return CheatSheetPlugin.getPlugin()
187:                                    .getImageRegistry().get(
188:                                            ICheatSheetResource.COMPOSITE_OBJ);
189:                        }
190:                        return CheatSheetPlugin.getPlugin().getImageRegistry()
191:                                .get(ICheatSheetResource.CHEATSHEET_OBJ);
192:                    }
193:                    return PlatformUI.getWorkbench().getSharedImages()
194:                            .getImage(ISharedImages.IMG_OBJ_FOLDER);
195:                }
196:            }
197:
198:            /**
199:             * Creates an instance of this dialog to display the a list of cheat sheets.
200:             * 
201:             * @param shell
202:             *            the parent shell
203:             */
204:            public CheatSheetCategoryBasedSelectionDialog(Shell shell,
205:                    CheatSheetCollectionElement cheatsheetCategories) {
206:                super (shell);
207:
208:                this .cheatsheetCategories = cheatsheetCategories;
209:
210:                this .title = Messages.CHEAT_SHEET_SELECTION_DIALOG_TITLE;
211:
212:                setShellStyle(getShellStyle() | SWT.RESIZE);
213:            }
214:
215:            /*
216:             * (non-Javadoc) Method declared on Window.
217:             */
218:            protected void configureShell(Shell newShell) {
219:                super .configureShell(newShell);
220:                if (title != null) {
221:                    newShell.setText(title);
222:                }
223:                newShell.setImage(CheatSheetPlugin.getPlugin().getImage(
224:                        ICheatSheetResource.CHEATSHEET_VIEW));
225:            }
226:
227:            /*
228:             * (non-Javadoc) Method declared on Dialog.
229:             */
230:            protected void createButtonsForButtonBar(Composite parent) {
231:                super .createButtonsForButtonBar(parent);
232:
233:                enableOKButton(okButtonState);
234:            }
235:
236:            /*
237:             * (non-Javadoc) Method declared on Dialog.
238:             */
239:            protected Control createDialogArea(Composite parent) {
240:                initializeDialogUnits(parent);
241:
242:                IDialogSettings workbenchSettings = CheatSheetPlugin
243:                        .getPlugin().getDialogSettings();
244:                IDialogSettings dialogSettings = workbenchSettings
245:                        .getSection(DIALOG_SETTINGS_SECTION);
246:                if (dialogSettings == null)
247:                    dialogSettings = workbenchSettings
248:                            .addNewSection(DIALOG_SETTINGS_SECTION);
249:
250:                setDialogSettings(dialogSettings);
251:
252:                // top level group
253:                Composite outerContainer = (Composite) super 
254:                        .createDialogArea(parent);
255:
256:                PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
257:                        CHEAT_SHEET_SELECTION_HELP_ID);
258:                GridLayout gridLayout = new GridLayout();
259:                gridLayout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
260:                gridLayout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
261:                gridLayout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
262:                gridLayout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
263:                outerContainer.setLayout(gridLayout);
264:                outerContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
265:
266:                // Create label
267:                createMessageArea(outerContainer);
268:
269:                // Create radio button
270:                selectRegisteredRadio = new Button(outerContainer, SWT.RADIO);
271:                selectRegisteredRadio
272:                        .setText(Messages.SELECTION_DIALOG_OPEN_REGISTERED);
273:
274:                SashForm sform = new SashForm(outerContainer, SWT.VERTICAL);
275:                GridData data = new GridData(GridData.FILL_BOTH);
276:                data.heightHint = 300;
277:                sform.setLayoutData(data);
278:
279:                // category tree pane
280:                treeViewer = new TreeViewer(sform, SWT.SINGLE | SWT.H_SCROLL
281:                        | SWT.V_SCROLL | SWT.BORDER);
282:                treeViewer.getTree().setLayoutData(data);
283:                treeViewer.setContentProvider(getCheatSheetProvider());
284:                treeViewer.setLabelProvider(new CheatsheetLabelProvider());
285:                treeViewer.setComparator(CheatSheetCollectionSorter.INSTANCE);
286:                treeViewer.addFilter(activityViewerFilter);
287:                treeViewer.addSelectionChangedListener(this );
288:                treeViewer.setInput(cheatsheetCategories);
289:
290:                desc = new Text(sform, SWT.MULTI | SWT.WRAP);
291:                desc.setEditable(false);
292:
293:                sform.setWeights(new int[] { 10, 2 });
294:
295:                if (activityViewerFilter.getHasEncounteredFilteredItem())
296:                    createShowAllButton(outerContainer);
297:
298:                // Add double-click listener
299:                treeViewer.addDoubleClickListener(new IDoubleClickListener() {
300:                    public void doubleClick(DoubleClickEvent event) {
301:                        IStructuredSelection selection = (IStructuredSelection) event
302:                                .getSelection();
303:                        Object obj = selection.getFirstElement();
304:                        if (obj instanceof  CheatSheetCollectionElement) {
305:                            boolean state = treeViewer.getExpandedState(obj);
306:                            treeViewer.setExpandedState(obj, !state);
307:                        } else {
308:                            okPressed();
309:                        }
310:                    }
311:                });
312:
313:                // Create radio button for select from file
314:                selectFileRadio = new Button(outerContainer, SWT.RADIO);
315:                selectFileRadio
316:                        .setText(Messages.SELECTION_DIALOG_OPEN_FROM_FILE);
317:
318:                Composite selectFileComposite = new Composite(outerContainer,
319:                        SWT.NULL);
320:                GridLayout selectFileLayout = new GridLayout();
321:                selectFileLayout.marginWidth = 0;
322:                selectFileLayout.marginHeight = 0;
323:                selectFileLayout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
324:                selectFileLayout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
325:                selectFileLayout.numColumns = 2;
326:                selectFileComposite.setLayout(selectFileLayout);
327:                GridData sfCompositeData = new GridData(
328:                        GridData.FILL_HORIZONTAL);
329:                sfCompositeData.widthHint = 300;
330:                selectFileComposite.setLayoutData(sfCompositeData);
331:                selectFileCombo = new Combo(selectFileComposite, SWT.BORDER);
332:                GridData sfTextData = new GridData(GridData.FILL_HORIZONTAL);
333:                selectFileCombo.setLayoutData(sfTextData);
334:                browseFileButton = new Button(selectFileComposite, SWT.NULL);
335:                browseFileButton
336:                        .setText(Messages.SELECTION_DIALOG_FILEPICKER_BROWSE);
337:                setButtonLayoutData(browseFileButton);
338:
339:                // Create radio button for select from URL
340:                selectUrlRadio = new Button(outerContainer, SWT.RADIO);
341:                selectUrlRadio.setText(Messages.SELECTION_DIALOG_OPEN_FROM_URL);
342:                selectUrlCombo = new Combo(outerContainer, SWT.BORDER);
343:                GridData suTextData = new GridData(GridData.FILL_HORIZONTAL);
344:                selectUrlCombo.setLayoutData(suTextData);
345:
346:                restoreWidgetValues();
347:                restoreFileSettings();
348:
349:                if (!treeViewer.getSelection().isEmpty())
350:                    // we only set focus if a selection was restored
351:                    treeViewer.getTree().setFocus();
352:
353:                Dialog.applyDialogFont(outerContainer);
354:                selectFileCombo.addModifyListener(new FileAndUrlListener());
355:                browseFileButton.addSelectionListener(new BrowseListener());
356:                selectRegisteredRadio
357:                        .addSelectionListener(new RadioSelectionListener());
358:                selectUrlRadio
359:                        .addSelectionListener(new RadioSelectionListener());
360:                selectUrlCombo.addModifyListener(new FileAndUrlListener());
361:                checkRadioButtons();
362:                return outerContainer;
363:            }
364:
365:            private class RadioSelectionListener implements  SelectionListener {
366:
367:                public void widgetSelected(SelectionEvent e) {
368:                    checkRadioButtons();
369:                }
370:
371:                public void widgetDefaultSelected(SelectionEvent e) {
372:                    // do nothing
373:                }
374:            }
375:
376:            private class BrowseListener implements  SelectionListener {
377:
378:                public void widgetSelected(SelectionEvent e) {
379:                    // Launch a file dialog to select a cheatsheet file
380:                    FileDialog fileDlg = new FileDialog(getShell());
381:                    fileDlg.setFilterExtensions(new String[] { "*.xml" }); //$NON-NLS-1$
382:                    fileDlg.setText(Messages.SELECTION_DIALOG_FILEPICKER_TITLE);
383:                    fileDlg.open();
384:                    String filename = fileDlg.getFileName();
385:                    if (filename != null) {
386:                        IPath folderPath = new Path(fileDlg.getFilterPath());
387:                        IPath filePath = folderPath.append(filename);
388:                        selectFileCombo.setText(filePath.toOSString());
389:                        checkRadioButtons();
390:                    }
391:                }
392:
393:                public void widgetDefaultSelected(SelectionEvent e) {
394:                    // do nothing			
395:                }
396:            }
397:
398:            private class FileAndUrlListener implements  ModifyListener {
399:
400:                public void modifyText(ModifyEvent e) {
401:                    setOkButton();
402:                }
403:            }
404:
405:            /*
406:             * Check the state of the Radio buttons and disable those parts of the UI that don't apply
407:             */
408:            private void checkRadioButtons() {
409:                selectFileCombo.setEnabled(selectFileRadio.getSelection());
410:                browseFileButton.setEnabled(selectFileRadio.getSelection());
411:                if (showAllButton != null) {
412:                    showAllButton.setEnabled(selectRegisteredRadio
413:                            .getSelection());
414:                }
415:                treeViewer.getTree().setEnabled(
416:                        selectRegisteredRadio.getSelection());
417:                selectUrlCombo.setEnabled(selectUrlRadio.getSelection());
418:                setOkButton();
419:            }
420:
421:            /**
422:             * Create a show all button in the parent.
423:             * 
424:             * @param parent
425:             *            the parent <code>Composite</code>.
426:             */
427:            private void createShowAllButton(Composite parent) {
428:                showAllButton = new Button(parent, SWT.CHECK);
429:                showAllButton
430:                        .setText(Messages.CheatSheetCategoryBasedSelectionDialog_showAll);
431:                showAllButton.addSelectionListener(new SelectionAdapter() {
432:
433:                    /*
434:                     * (non-Javadoc)
435:                     * 
436:                     * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
437:                     */
438:                    public void widgetSelected(SelectionEvent e) {
439:                        if (showAllButton.getSelection()) {
440:                            treeViewer.resetFilters();
441:                        } else {
442:                            treeViewer.addFilter(activityViewerFilter);
443:                        }
444:                    }
445:                });
446:            }
447:
448:            /**
449:             * Method enableOKButton enables/diables the OK button for the dialog and
450:             * saves the state, allowing the enabling/disabling to occur even if the
451:             * button has not been created yet.
452:             * 
453:             * @param value
454:             */
455:            private void enableOKButton(boolean value) {
456:                Button button = getButton(IDialogConstants.OK_ID);
457:
458:                okButtonState = value;
459:                if (button != null) {
460:                    button.setEnabled(value);
461:                }
462:            }
463:
464:            /**
465:             * Expands the cheatsheet categories in this page's category viewer that
466:             * were expanded last time this page was used. If a category that was
467:             * previously expanded no longer exists then it is ignored.
468:             */
469:            protected CheatSheetCollectionElement expandPreviouslyExpandedCategories() {
470:                String[] expandedCategoryPaths = settings
471:                        .getArray(STORE_EXPANDED_CATEGORIES_ID);
472:                List categoriesToExpand = new ArrayList(
473:                        expandedCategoryPaths.length);
474:
475:                for (int i = 0; i < expandedCategoryPaths.length; i++) {
476:                    CheatSheetCollectionElement category = cheatsheetCategories
477:                            .findChildCollection(new Path(
478:                                    expandedCategoryPaths[i]));
479:                    if (category != null) // ie.- it still exists
480:                        categoriesToExpand.add(category);
481:                }
482:
483:                if (!categoriesToExpand.isEmpty())
484:                    treeViewer
485:                            .setExpandedElements(categoriesToExpand.toArray());
486:                return categoriesToExpand.isEmpty() ? null
487:                        : (CheatSheetCollectionElement) categoriesToExpand
488:                                .get(categoriesToExpand.size() - 1);
489:            }
490:
491:            /**
492:             * Returns the content provider for this page.
493:             */
494:            protected IContentProvider getCheatSheetProvider() {
495:                // want to get the cheatsheets of the collection element
496:                return new BaseWorkbenchContentProvider() {
497:                    public Object[] getChildren(Object o) {
498:                        Object[] cheatsheets;
499:                        if (o instanceof  CheatSheetCollectionElement) {
500:                            cheatsheets = ((CheatSheetCollectionElement) o)
501:                                    .getCheatSheets();
502:                        } else {
503:                            cheatsheets = new Object[0];
504:                        }
505:                        Object[] subCategories = super .getChildren(o);
506:                        if (cheatsheets.length == 0) {
507:                            return subCategories;
508:                        } else if (subCategories.length == 0) {
509:                            return cheatsheets;
510:                        } else {
511:                            Object[] result = new Object[cheatsheets.length
512:                                    + subCategories.length];
513:                            System.arraycopy(subCategories, 0, result, 0,
514:                                    subCategories.length);
515:                            System.arraycopy(cheatsheets, 0, result,
516:                                    subCategories.length, cheatsheets.length);
517:                            return result;
518:                        }
519:                    }
520:                };
521:            }
522:
523:            /**
524:             * Returns the single selected object contained in the passed
525:             * selectionEvent, or <code>null</code> if the selectionEvent contains
526:             * either 0 or 2+ selected objects.
527:             */
528:            protected Object getSingleSelection(ISelection selection) {
529:                IStructuredSelection ssel = (IStructuredSelection) selection;
530:                return ssel.size() == 1 ? ssel.getFirstElement() : null;
531:            }
532:
533:            /**
534:             * The user selected either new cheatsheet category(s) or cheatsheet
535:             * element(s). Proceed accordingly.
536:             * 
537:             * @param newSelection
538:             *            ISelection
539:             */
540:            public void selectionChanged(SelectionChangedEvent selectionEvent) {
541:                Object obj = getSingleSelection(selectionEvent.getSelection());
542:                if (obj instanceof  CheatSheetCollectionElement) {
543:                    currentSelection = null;
544:                } else {
545:                    currentSelection = (CheatSheetElement) obj;
546:                }
547:
548:                if (currentSelection != null) {
549:                    desc.setText(currentSelection.getDescription());
550:                } else {
551:                    desc.setText(""); //$NON-NLS-1$
552:                }
553:                setOkButton();
554:            }
555:
556:            private void setOkButton() {
557:                if (selectRegisteredRadio.getSelection()) {
558:                    enableOKButton(currentSelection != null);
559:                } else if (selectFileRadio.getSelection()) {
560:                    enableOKButton(selectFileCombo.getText().length() > 0);
561:                } else {
562:                    enableOKButton(selectUrlCombo.getText().length() > 0);
563:                }
564:            }
565:
566:            /*
567:             * (non-Javadoc) Method declared on Dialog.
568:             */
569:            protected void okPressed() {
570:                /*
571:                 * Prevent the cheat sheet from opening inside this dialog's tray
572:                 * because it is about to close.
573:                 */
574:                getShell().setVisible(false);
575:
576:                if (selectFileRadio.getSelection()) {
577:                    setResultFromFile();
578:                } else if (selectRegisteredRadio.getSelection()) {
579:                    setResultFromTree();
580:                } else {
581:                    setResultFromUrl();
582:                }
583:
584:                // save our selection state
585:                saveWidgetValues();
586:
587:                super .okPressed();
588:            }
589:
590:            private void setResultFromTree() {
591:                if (currentSelection != null) {
592:                    ITriggerPoint triggerPoint = PlatformUI.getWorkbench()
593:                            .getActivitySupport().getTriggerPointManager()
594:                            .getTriggerPoint(
595:                                    ICheatSheetResource.TRIGGER_POINT_ID);
596:                    if (WorkbenchActivityHelper.allowUseOf(triggerPoint,
597:                            currentSelection)) {
598:                        new OpenCheatSheetAction(currentSelection.getID())
599:                                .run();
600:                    }
601:                }
602:            }
603:
604:            private void setResultFromFile() {
605:                // Use the filename without extension as the id of this cheatsheet
606:                IPath filePath = new Path(selectFileCombo.getText());
607:                String id = filePath.lastSegment();
608:                int extensionIndex = id.indexOf('.');
609:                if (extensionIndex > 0) {
610:                    id = id.substring(0, extensionIndex);
611:                }
612:                // Use the id as the name 
613:                URL url = null;
614:                boolean opened = false;
615:
616:                try {
617:                    File contentFile = new File(selectFileCombo.getText());
618:                    url = contentFile.toURL();
619:                    new OpenCheatSheetAction(id, id, url).run();
620:                    opened = true;
621:                } catch (MalformedURLException e) {
622:                    opened = false;
623:                }
624:                if (!opened) {
625:                    String message = NLS.bind(Messages.ERROR_OPENING_FILE,
626:                            (new Object[] { selectFileCombo.getText() }));
627:                    status = new Status(IStatus.ERROR,
628:                            ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,
629:                            ParserStatusUtility.PARSER_ERROR, message, null);
630:                    CheatSheetView view = ViewUtilities.showCheatSheetView();
631:                    view.getCheatSheetViewer().showError(message);
632:                }
633:            }
634:
635:            private void setResultFromUrl() {
636:                // Use the filename without extension as the id of this cheatsheet
637:                IPath filePath = new Path(selectUrlCombo.getText());
638:                String id = filePath.lastSegment();
639:                int extensionIndex = id.indexOf('.');
640:                if (extensionIndex > 0) {
641:                    id = id.substring(0, extensionIndex);
642:                }
643:                // Use the id as the name
644:                URL url = null;
645:                boolean opened = false;
646:                CheatSheetView view = ViewUtilities.showCheatSheetView();
647:                if (view == null) {
648:                    return;
649:                }
650:                try {
651:                    url = new URL(selectUrlCombo.getText());
652:                    view.getCheatSheetViewer().setInput(id, id, url,
653:                            new DefaultStateManager(), true);
654:                    opened = true;
655:                } catch (MalformedURLException e) {
656:                    opened = false;
657:                }
658:                if (!opened) {
659:                    String message = NLS.bind(Messages.ERROR_OPENING_FILE,
660:                            (new Object[] { selectUrlCombo.getText() }));
661:                    status = new Status(IStatus.ERROR,
662:                            ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,
663:                            ParserStatusUtility.PARSER_ERROR, message, null);
664:                    view.getCheatSheetViewer().showError(message);
665:                }
666:            }
667:
668:            /**
669:             * Set's widgets to the values that they held last time this page was
670:             * opened
671:             */
672:            protected void restoreWidgetValues() {
673:                String[] expandedCategoryPaths = settings
674:                        .getArray(STORE_EXPANDED_CATEGORIES_ID);
675:                if (expandedCategoryPaths == null)
676:                    return; // no stored values
677:
678:                CheatSheetCollectionElement category = expandPreviouslyExpandedCategories();
679:                if (category != null)
680:                    selectPreviouslySelectedCheatSheet(category);
681:            }
682:
683:            /**
684:             * Restores the state of the radio button and file name fields
685:             */
686:            private void restoreFileSettings() {
687:                int radioSetting = RADIO_REGISTERED;
688:                try {
689:                    radioSetting = settings.getInt(STORE_RADIO_SETTING);
690:                } catch (NumberFormatException n) {
691:                }
692:                selectFileRadio.setSelection(radioSetting == RADIO_FILE);
693:                selectRegisteredRadio
694:                        .setSelection(radioSetting == RADIO_REGISTERED);
695:                selectUrlRadio.setSelection(radioSetting == RADIO_URL);
696:                String fileName = settings.get(STORE_CHEATSHEET_FILENAME);
697:                if (fileName != null) {
698:                    selectFileCombo.setText(fileName);
699:                }
700:                String url = settings.get(STORE_CHEATSHEET_URL);
701:                if (url != null) {
702:                    selectUrlCombo.setText(url);
703:                }
704:                loadMRU(mostRecentUrls, STORE_URL_MRU, selectUrlCombo);
705:                loadMRU(mostRecentFiles, STORE_FILE_MRU, selectFileCombo);
706:            }
707:
708:            private void loadMRU(List mostRecentList, String key, Combo combo) {
709:                for (int i = 0; i < MOST_RECENT_LENGTH; i++) {
710:                    String name = settings.get(key + i);
711:                    if (name != null) {
712:                        mostRecentList.add(name);
713:                        combo.add(name);
714:                    }
715:                }
716:            }
717:
718:            private void saveMRU(List mostRecentList, String key,
719:                    String selection) {
720:                if (selection.length() > 0
721:                        && !mostRecentList.contains(selection)) {
722:                    mostRecentList.add(0, selection);
723:                }
724:                for (int i = 0; i < MOST_RECENT_LENGTH
725:                        & i < mostRecentList.size(); i++) {
726:                    String name = (String) mostRecentList.get(i);
727:                    if (name.length() > 0) {
728:                        settings.put(key + i, name);
729:                    }
730:                }
731:            }
732:
733:            /**
734:             * Store the current values of self's widgets so that they can be restored
735:             * in the next instance of self
736:             * 
737:             */
738:            public void saveWidgetValues() {
739:                storeExpandedCategories();
740:                storeSelectedCheatSheet();
741:                storeFileSettings();
742:            }
743:
744:            /**
745:             * Selects the cheatsheet category and cheatsheet in this page that were
746:             * selected last time this page was used. If a category or cheatsheet that
747:             * was previously selected no longer exists then it is ignored.
748:             */
749:            protected void selectPreviouslySelectedCheatSheet(
750:                    CheatSheetCollectionElement category) {
751:                String cheatsheetId = settings
752:                        .get(STORE_SELECTED_CHEATSHEET_ID);
753:                if (cheatsheetId == null)
754:                    return;
755:                CheatSheetElement cheatsheet = category.findCheatSheet(
756:                        cheatsheetId, false);
757:                if (cheatsheet == null)
758:                    return; // cheatsheet no longer exists, or has moved
759:
760:                treeViewer.setSelection(new StructuredSelection(cheatsheet));
761:            }
762:
763:            /**
764:             * Set the dialog store to use for widget value storage and retrieval
765:             * 
766:             * @param settings
767:             *            IDialogSettings
768:             */
769:            public void setDialogSettings(IDialogSettings settings) {
770:                this .settings = settings;
771:            }
772:
773:            /**
774:             * Stores the collection of currently-expanded categories in this page's
775:             * dialog store, in order to recreate this page's state in the next instance
776:             * of this page.
777:             */
778:            protected void storeExpandedCategories() {
779:                Object[] expandedElements = treeViewer.getExpandedElements();
780:                String[] expandedElementPaths = new String[expandedElements.length];
781:                for (int i = 0; i < expandedElements.length; ++i) {
782:                    expandedElementPaths[i] = ((CheatSheetCollectionElement) expandedElements[i])
783:                            .getPath().toString();
784:                }
785:                settings
786:                        .put(STORE_EXPANDED_CATEGORIES_ID, expandedElementPaths);
787:            }
788:
789:            /**
790:             * Stores the currently-selected category and cheatsheet in this page's
791:             * dialog store, in order to recreate this page's state in the next instance
792:             * of this page.
793:             */
794:            protected void storeSelectedCheatSheet() {
795:                CheatSheetElement element = null;
796:
797:                Object el = getSingleSelection(treeViewer.getSelection());
798:                if (el == null)
799:                    return;
800:
801:                if (el instanceof  CheatSheetElement) {
802:                    element = (CheatSheetElement) el;
803:                } else
804:                    return;
805:
806:                settings.put(STORE_SELECTED_CHEATSHEET_ID, element.getID());
807:            }
808:
809:            /**
810:             * Stores the state of the radio button and file name fields
811:             */
812:            private void storeFileSettings() {
813:                int radioSetting = 0;
814:                if (selectRegisteredRadio.getSelection()) {
815:                    radioSetting = 1;
816:                }
817:                if (selectFileRadio.getSelection()) {
818:                    radioSetting = 2;
819:                }
820:                if (selectUrlRadio.getSelection()) {
821:                    radioSetting = 3;
822:                }
823:                settings.put(STORE_RADIO_SETTING, radioSetting);
824:                settings.put(STORE_CHEATSHEET_FILENAME, selectFileCombo
825:                        .getText());
826:                settings.put(STORE_CHEATSHEET_URL, selectUrlCombo.getText());
827:
828:                saveMRU(mostRecentUrls, STORE_URL_MRU, selectUrlCombo.getText());
829:                saveMRU(mostRecentFiles, STORE_FILE_MRU, selectFileCombo
830:                        .getText());
831:            }
832:
833:            /* (non-Javadoc)
834:             * @see org.eclipse.jface.window.Dialog#getDialogBoundsSettings()
835:             * 
836:             * @since 3.2
837:             */
838:            protected IDialogSettings getDialogBoundsSettings() {
839:                IDialogSettings settings = CheatSheetPlugin.getPlugin()
840:                        .getDialogSettings();
841:                IDialogSettings section = settings
842:                        .getSection(DIALOG_SETTINGS_SECTION);
843:                if (section == null) {
844:                    section = settings.addNewSection(DIALOG_SETTINGS_SECTION);
845:                }
846:                return section;
847:            }
848:
849:            private Label createMessageArea(Composite composite) {
850:                Label label = new Label(composite, SWT.NONE);
851:                label.setText(Messages.CHEAT_SHEET_SELECTION_DIALOG_MSG);
852:                label.setFont(composite.getFont());
853:                return label;
854:            }
855:
856:            public IStatus getStatus() {
857:                return status;
858:            }
859:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.