KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > BaseBlock


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.pde.internal.ui.launcher;
12
13 import org.eclipse.core.resources.IContainer;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.resources.IWorkspaceRoot;
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.core.variables.IStringVariableManager;
21 import org.eclipse.core.variables.VariablesPlugin;
22 import org.eclipse.debug.ui.StringVariableSelectionDialog;
23 import org.eclipse.jface.window.Window;
24 import org.eclipse.osgi.util.NLS;
25 import org.eclipse.pde.internal.ui.PDEUIMessages;
26 import org.eclipse.pde.internal.ui.util.SWTUtil;
27 import org.eclipse.pde.ui.launcher.AbstractLauncherTab;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.ModifyEvent;
30 import org.eclipse.swt.events.ModifyListener;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.DirectoryDialog;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Text;
39 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
40
41 public abstract class BaseBlock {
42     
43     protected AbstractLauncherTab fTab;
44
45     private Button fVariablesButton;
46     private Button fFileSystemButton;
47     private Button fWorkspaceButton;
48     
49     protected Text fLocationText;
50     
51     protected Listener fListener = new Listener();
52
53     protected Label fLocationLabel;
54
55     class Listener extends SelectionAdapter implements ModifyListener {
56         public void widgetSelected(SelectionEvent e) {
57             Object JavaDoc source= e.getSource();
58             if (source == fFileSystemButton) {
59                 handleBrowseFileSystem();
60             } else if (source == fWorkspaceButton) {
61                 handleBrowseWorkspace();
62             } else if (source == fVariablesButton) {
63                 handleInsertVariable();
64             } else {
65                 fTab.updateLaunchConfigurationDialog();
66             }
67         }
68
69         public void modifyText(ModifyEvent e) {
70             fTab.updateLaunchConfigurationDialog();
71         }
72     }
73     
74     public BaseBlock(AbstractLauncherTab tab) {
75         fTab = tab;
76     }
77     
78     protected void createText(Composite parent, String JavaDoc text, int indent) {
79         fLocationLabel = new Label(parent, SWT.NONE);
80         fLocationLabel.setText(text);
81         if (indent > 0) {
82             GridData gd = new GridData();
83             gd.horizontalIndent = indent;
84             fLocationLabel.setLayoutData(gd);
85         }
86
87         fLocationText = new Text(parent, SWT.SINGLE|SWT.BORDER);
88         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
89         gd.widthHint = 400;
90         fLocationText.setLayoutData(gd);
91         fLocationText.addModifyListener(fListener);
92     }
93     
94     protected void createButtons(Composite parent, String JavaDoc[] buttonLabels) {
95         fWorkspaceButton = createButton(parent, buttonLabels[0]);
96         fFileSystemButton = createButton(parent, buttonLabels[1]);
97         fVariablesButton = createButton(parent, buttonLabels[2]);
98     }
99     
100     protected Button createButton(Composite parent, String JavaDoc text) {
101         Button button = new Button(parent, SWT.PUSH);
102         button.setText(text);
103         button.setLayoutData(new GridData());
104         button.addSelectionListener(fListener);
105         SWTUtil.setButtonDimensionHint(button);
106         return button;
107     }
108         
109     protected void handleBrowseFileSystem() {
110         DirectoryDialog dialog = new DirectoryDialog(fTab.getControl().getShell());
111         dialog.setFilterPath(getLocation());
112         dialog.setText(PDEUIMessages.BaseBlock_dirSelection);
113         dialog.setMessage(PDEUIMessages.BaseBlock_dirChoose);
114         String JavaDoc result = dialog.open();
115         if (result != null)
116             fLocationText.setText(result);
117     }
118     
119     protected void handleBrowseWorkspace() {
120         ContainerSelectionDialog dialog =
121             new ContainerSelectionDialog(
122                     fTab.getControl().getShell(),
123                     getContainer(),
124                     true,
125                     PDEUIMessages.BaseBlock_relative);
126         if (dialog.open() == Window.OK) {
127             Object JavaDoc[] result = dialog.getResult();
128             if (result.length == 0)
129                 return;
130             IPath path = (IPath)result[0];
131             fLocationText.setText("${workspace_loc:" + path.makeRelative().toString() + "}"); //$NON-NLS-1$ //$NON-NLS-2$
132
}
133     }
134     
135     protected IContainer getContainer() {
136         String JavaDoc path = getLocation();
137         if (path.length() > 0) {
138             IResource res = null;
139             IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
140             if (path.startsWith("${workspace_loc:")) { //$NON-NLS-1$
141
IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager();
142                 try {
143                     path = manager.performStringSubstitution(path, false);
144                     IContainer[] containers = root.findContainersForLocation(new Path(path));
145                     if (containers.length > 0)
146                         res = containers[0];
147                 } catch (CoreException e) {
148                 }
149             } else {
150                 res = root.findMember(path);
151             }
152             if (res instanceof IContainer) {
153                 return (IContainer)res;
154             }
155         }
156         return ResourcesPlugin.getWorkspace().getRoot();
157     }
158     
159     private void handleInsertVariable() {
160         StringVariableSelectionDialog dialog =
161                     new StringVariableSelectionDialog(fTab.getControl().getShell());
162         if (dialog.open() == Window.OK)
163             fLocationText.insert(dialog.getVariableExpression());
164     }
165     
166     protected String JavaDoc getLocation() {
167         return fLocationText.getText().trim();
168     }
169     
170     public String JavaDoc validate() {
171         return (fLocationText.isEnabled() && getLocation().length() == 0)
172                     ? NLS.bind(PDEUIMessages.BaseBlock_errorMessage, getName())
173                     : null;
174     }
175     
176     protected abstract String JavaDoc getName();
177     
178     protected void enableBrowseSection(boolean enabled) {
179         fLocationLabel.setEnabled(enabled);
180         fLocationText.setEnabled(enabled);
181         fFileSystemButton.setEnabled(enabled);
182         fWorkspaceButton.setEnabled(enabled);
183         fVariablesButton.setEnabled(enabled);
184         fTab.updateLaunchConfigurationDialog();
185     }
186     
187 }
188
Popular Tags