KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > views > ScopePreferenceDialog


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.help.ui.internal.views;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.help.ui.internal.Messages;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.jface.preference.*;
18 import org.eclipse.jface.viewers.*;
19 import org.eclipse.jface.viewers.TreeViewer;
20 import org.eclipse.jface.wizard.WizardDialog;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.layout.*;
24 import org.eclipse.swt.widgets.*;
25
26 public class ScopePreferenceDialog extends PreferenceDialog {
27     private EngineDescriptorManager descManager;
28     private ArrayList JavaDoc pendingOperations;
29     
30     class PendingOperation {
31         int action;
32         EngineDescriptor desc;
33         public PendingOperation(int action, EngineDescriptor desc) {
34             this.action = action;
35             this.desc = desc;
36         }
37     }
38     /**
39      * The Add button id.
40      */

41     private final static int NEW_ID = IDialogConstants.CLIENT_ID + 1;
42
43     /**
44      * The Remove button id.
45      */

46     private final static int DELETE_ID = IDialogConstants.CLIENT_ID + 2;
47     
48     public ScopePreferenceDialog(Shell parentShell, PreferenceManager manager, EngineDescriptorManager descManager, boolean editable) {
49         super(parentShell, manager);
50         this.descManager = descManager;
51     }
52
53     protected Control createTreeAreaContents(Composite parent) {
54         GridLayout layout = new GridLayout();
55         layout.marginWidth = layout.marginHeight = 0;
56         Composite container = new Composite(parent, SWT.NULL);
57         container.setLayout(layout);
58         Control treeControl = super.createTreeAreaContents(container);
59         GridData treeGd = (GridData)treeControl.getLayoutData();
60         GridData gd = new GridData(GridData.FILL_BOTH);
61         gd.horizontalSpan = 2;
62         treeControl.setLayoutData(gd);
63
64         Button lbutton = createButton(container, NEW_ID, Messages.ScopePreferenceDialog_new, false);
65         gd = (GridData)lbutton.getLayoutData();
66         gd.horizontalAlignment = GridData.HORIZONTAL_ALIGN_BEGINNING;
67         Button rbutton = createButton(container, DELETE_ID, Messages.ScopePreferenceDialog_delete, false);
68         rbutton.setEnabled(false);
69         gd = (GridData)rbutton.getLayoutData();
70         gd.horizontalAlignment = GridData.HORIZONTAL_ALIGN_BEGINNING;
71         layout.numColumns = 2;
72         container.setLayoutData(treeGd);
73         Point size = container.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
74         treeGd.widthHint = Math.max(treeGd.widthHint, size.x);
75         return container;
76     }
77     
78     protected TreeViewer createTreeViewer(Composite parent) {
79         TreeViewer viewer = super.createTreeViewer(parent);
80         viewer.addSelectionChangedListener(new ISelectionChangedListener() {
81             public void selectionChanged(SelectionChangedEvent event) {
82                 IStructuredSelection ssel = (IStructuredSelection)event.getSelection();
83                 Object JavaDoc obj = ssel.getFirstElement();
84                 treeSelectionChanged(obj);
85             }
86         });
87         return viewer;
88     }
89     
90     protected void buttonPressed(int buttonId) {
91         switch (buttonId) {
92         case NEW_ID:
93             doNew();
94             break;
95         case DELETE_ID:
96             doDelete();
97             break;
98         default:
99             super.buttonPressed(buttonId);
100         }
101     }
102     
103     private void treeSelectionChanged(Object JavaDoc obj) {
104         boolean removable = false;
105         if (obj instanceof ScopePreferenceManager.EnginePreferenceNode) {
106             ScopePreferenceManager.EnginePreferenceNode node = (ScopePreferenceManager.EnginePreferenceNode)obj;
107             EngineDescriptor desc = node.getDescriptor();
108             removable = desc.isUserDefined();
109         }
110         getButton(DELETE_ID).setEnabled(removable);
111     }
112     
113     private void doNew() {
114         NewEngineWizard wizard = new NewEngineWizard(descManager.getEngineTypes());
115         WizardDialog dialog = new WizardDialog(getShell(), wizard);
116         dialog.create();
117         dialog.getShell().setSize(400, 500);
118         if (dialog.open()==WizardDialog.OK) {
119             EngineTypeDescriptor etdesc = wizard.getSelectedEngineType();
120             EngineDescriptor desc = new EngineDescriptor(descManager);
121             desc.setEngineType(etdesc);
122             desc.setUserDefined(true);
123             desc.setId(descManager.computeNewId(etdesc.getId()));
124             ScopePreferenceManager mng = (ScopePreferenceManager)getPreferenceManager();
125             IPreferenceNode node = mng.addNode(desc);
126             getTreeViewer().refresh();
127             getTreeViewer().setSelection(new StructuredSelection(node));
128             scheduleOperation(NEW_ID, desc);
129         }
130     }
131
132     private void doDelete() {
133         Object JavaDoc obj = ((IStructuredSelection)getTreeViewer().getSelection()).getFirstElement();
134         if (obj instanceof ScopePreferenceManager.EnginePreferenceNode) {
135             ScopePreferenceManager.EnginePreferenceNode node = (ScopePreferenceManager.EnginePreferenceNode)obj;
136             EngineDescriptor desc = node.getDescriptor();
137             //ScopePreferenceManager mng = (ScopePreferenceManager)getPreferenceManager();
138
getTreeViewer().remove(node);
139             scheduleOperation(DELETE_ID, desc);
140         }
141     }
142     
143     private void scheduleOperation(int action, EngineDescriptor desc) {
144         if (pendingOperations==null)
145             pendingOperations = new ArrayList JavaDoc();
146         pendingOperations.add(new PendingOperation(action, desc));
147     }
148     protected void okPressed() {
149         super.okPressed();
150         if (pendingOperations!=null) {
151             // process pending operations
152
for (int i=0; i<pendingOperations.size(); i++) {
153                 PendingOperation op = (PendingOperation)pendingOperations.get(i);
154                 if (op.action==NEW_ID)
155                     descManager.add(op.desc);
156                 else
157                     descManager.remove(op.desc);
158             }
159             pendingOperations = null;
160         }
161         descManager.save();
162     }
163 }
Popular Tags