001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.dso.editors;
006:
007: import org.apache.xmlbeans.XmlObject;
008: import org.eclipse.core.resources.IProject;
009: import org.eclipse.jdt.core.IJavaElement;
010: import org.eclipse.jdt.core.IJavaProject;
011: import org.eclipse.jdt.core.IType;
012: import org.eclipse.jdt.core.JavaCore;
013: import org.eclipse.jdt.core.JavaModelException;
014: import org.eclipse.jdt.core.search.IJavaSearchScope;
015: import org.eclipse.jdt.core.search.SearchEngine;
016: import org.eclipse.jdt.ui.IJavaElementSearchConstants;
017: import org.eclipse.jdt.ui.JavaUI;
018: import org.eclipse.swt.SWT;
019: import org.eclipse.swt.events.SelectionAdapter;
020: import org.eclipse.swt.events.SelectionEvent;
021: import org.eclipse.swt.layout.GridData;
022: import org.eclipse.swt.layout.GridLayout;
023: import org.eclipse.swt.widgets.Button;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Event;
026: import org.eclipse.swt.widgets.Label;
027: import org.eclipse.swt.widgets.Listener;
028: import org.eclipse.swt.widgets.Table;
029: import org.eclipse.swt.widgets.TableColumn;
030: import org.eclipse.swt.widgets.TableItem;
031: import org.eclipse.ui.dialogs.SelectionDialog;
032: import org.terracotta.dso.PatternHelper;
033: import org.terracotta.ui.util.SWTUtil;
034:
035: import com.terracottatech.config.AdditionalBootJarClasses;
036: import com.terracottatech.config.DsoApplication;
037: import com.terracottatech.config.QualifiedClassName;
038:
039: public class BootClassesPanel extends ConfigurationEditorPanel {
040: private IProject m_project;
041: private DsoApplication m_dsoApp;
042: private AdditionalBootJarClasses m_bootClasses;
043:
044: private final Layout m_layout;
045:
046: private AddHandler m_addHandler;
047: private RemoveHandler m_removeHandler;
048: private TableSelectionListener m_tableSelectionListener;
049: private TableDataListener m_tableDataListener;
050:
051: private static final String CLASS_SELECT_TITLE = "DSO Application Configuration";
052: private static final String CLASS_SELECT_MESSAGE = "Select system classes to add to DSO Boot Jar";
053:
054: public BootClassesPanel(Composite parent, int style) {
055: super (parent, style);
056: m_layout = new Layout(this );
057: m_addHandler = new AddHandler();
058: m_removeHandler = new RemoveHandler();
059: m_tableSelectionListener = new TableSelectionListener();
060: m_tableDataListener = new TableDataListener();
061: }
062:
063: public boolean hasAnySet() {
064: return m_bootClasses != null
065: && m_bootClasses.sizeOfIncludeArray() > 0;
066: }
067:
068: private AdditionalBootJarClasses ensureAdditionalBootClasses() {
069: if (m_bootClasses == null) {
070: ensureXmlObject();
071: }
072: return m_bootClasses;
073: }
074:
075: public void ensureXmlObject() {
076: super .ensureXmlObject();
077:
078: if (m_bootClasses == null) {
079: removeListeners();
080: m_bootClasses = m_dsoApp.addNewAdditionalBootJarClasses();
081: updateChildren();
082: addListeners();
083: }
084: }
085:
086: private void testRemoveBootClasses() {
087: if (!hasAnySet()
088: && m_dsoApp.getAdditionalBootJarClasses() != null) {
089: m_dsoApp.unsetAdditionalBootJarClasses();
090: m_bootClasses = null;
091: fireXmlObjectStructureChanged(m_dsoApp);
092: }
093: fireBootClassesChanged();
094: testDisableRemoveButton();
095: }
096:
097: private void testDisableRemoveButton() {
098: m_layout.m_removeButton.setEnabled(m_layout.m_table
099: .getSelectionCount() > 0);
100: }
101:
102: private void addListeners() {
103: m_layout.m_addButton.addSelectionListener(m_addHandler);
104: m_layout.m_removeButton.addSelectionListener(m_removeHandler);
105: m_layout.m_table.addSelectionListener(m_tableSelectionListener);
106: m_layout.m_table.addListener(SWT.SetData, m_tableDataListener);
107: }
108:
109: private void removeListeners() {
110: m_layout.m_addButton.removeSelectionListener(m_addHandler);
111: m_layout.m_removeButton
112: .removeSelectionListener(m_removeHandler);
113: m_layout.m_table
114: .removeSelectionListener(m_tableSelectionListener);
115: m_layout.m_table.removeListener(SWT.SetData,
116: m_tableDataListener);
117: }
118:
119: public void updateChildren() {
120: initTableItems();
121: testDisableRemoveButton();
122: }
123:
124: public void updateModel() {
125: removeListeners();
126: updateChildren();
127: addListeners();
128: }
129:
130: public void setup(IProject project, DsoApplication dsoApp) {
131: setEnabled(true);
132: removeListeners();
133:
134: m_project = project;
135: m_dsoApp = dsoApp;
136: m_bootClasses = m_dsoApp != null ? m_dsoApp
137: .getAdditionalBootJarClasses() : null;
138:
139: updateChildren();
140: addListeners();
141: }
142:
143: public void tearDown() {
144: removeListeners();
145:
146: m_dsoApp = null;
147: m_bootClasses = null;
148:
149: setEnabled(false);
150: }
151:
152: private void initTableItems() {
153: m_layout.m_table.removeAll();
154: if (m_bootClasses == null)
155: return;
156: XmlObject[] includes = m_bootClasses.selectPath("*");
157: for (int i = 0; i < includes.length; i++) {
158: createTableItem((QualifiedClassName) includes[i]);
159: }
160: if (includes.length > 0) {
161: m_layout.m_table.setSelection(0);
162: }
163: }
164:
165: private void createTableItem(QualifiedClassName element) {
166: TableItem item = new TableItem(m_layout.m_table, SWT.NONE);
167: item.setText(element.getStringValue());
168: item.setData(element);
169: }
170:
171: private void internalAddBootClass(String typeName) {
172: AdditionalBootJarClasses bootClasses = ensureAdditionalBootClasses();
173: bootClasses.addInclude(typeName);
174: createTableItem(bootClasses.xgetIncludeArray(bootClasses
175: .sizeOfIncludeArray() - 1));
176:
177: int row = m_layout.m_table.getItemCount() - 1;
178: m_layout.m_table.setSelection(row);
179: }
180:
181: public boolean isBootClass(String typeName) {
182: AdditionalBootJarClasses bootClasses = ensureAdditionalBootClasses();
183:
184: for (int i = 0; i < bootClasses.sizeOfIncludeArray(); i++) {
185: if (typeName.equals(bootClasses.getIncludeArray(i))) {
186: return true;
187: }
188: }
189:
190: return false;
191: }
192:
193: private static class Layout {
194: private static final String BOOT_CLASSES = "Boot Classes";
195: private static final String ADD = "Add...";
196: private static final String REMOVE = "Remove";
197:
198: private Button m_addButton;
199: private Button m_removeButton;
200: private Table m_table;
201:
202: public void reset() {
203: m_removeButton.setEnabled(false);
204: m_table.removeAll();
205: }
206:
207: private Layout(Composite parent) {
208: Composite comp = new Composite(parent, SWT.NONE);
209: GridLayout gridLayout = new GridLayout(2, false);
210: gridLayout.marginWidth = gridLayout.marginHeight = 10;
211: comp.setLayout(gridLayout);
212:
213: Composite sidePanel = new Composite(comp, SWT.NONE);
214: gridLayout = new GridLayout();
215: gridLayout.marginWidth = gridLayout.marginHeight = 0;
216: sidePanel.setLayout(gridLayout);
217: sidePanel.setLayoutData(new GridData(GridData.FILL_BOTH));
218:
219: Label label = new Label(sidePanel, SWT.NONE);
220: label.setText(BOOT_CLASSES);
221: label.setLayoutData(new GridData(
222: GridData.VERTICAL_ALIGN_BEGINNING));
223:
224: m_table = new Table(sidePanel, SWT.BORDER | SWT.MULTI
225: | SWT.FULL_SELECTION | SWT.V_SCROLL);
226: m_table.setHeaderVisible(true);
227: m_table.setLinesVisible(true);
228: SWTUtil.makeTableColumnsResizeWeightedWidth(sidePanel,
229: m_table, new int[] { 3, 1 });
230: SWTUtil.makeTableColumnsEditable(m_table, new int[] { 0 });
231: GridData gridData = new GridData(GridData.FILL_BOTH);
232: gridData.heightHint = SWTUtil.tableRowsToPixels(m_table, 3);
233: m_table.setLayoutData(gridData);
234:
235: TableColumn column = new TableColumn(m_table, SWT.NONE);
236: column.setText(BOOT_CLASSES);
237: column.pack();
238:
239: Composite buttonPanel = new Composite(comp, SWT.NONE);
240: gridLayout = new GridLayout();
241: gridLayout.marginWidth = gridLayout.marginHeight = 0;
242: buttonPanel.setLayout(gridLayout);
243: buttonPanel.setLayoutData(new GridData(
244: GridData.VERTICAL_ALIGN_BEGINNING));
245:
246: new Label(buttonPanel, SWT.NONE); // filler
247:
248: m_addButton = new Button(buttonPanel, SWT.PUSH);
249: m_addButton.setText(ADD);
250: m_addButton.setLayoutData(new GridData(
251: GridData.VERTICAL_ALIGN_END));
252: SWTUtil.applyDefaultButtonSize(m_addButton);
253:
254: m_removeButton = new Button(buttonPanel, SWT.PUSH);
255: m_removeButton.setText(REMOVE);
256: m_removeButton.setEnabled(false);
257: m_removeButton.setLayoutData(new GridData(
258: GridData.VERTICAL_ALIGN_BEGINNING));
259: SWTUtil.applyDefaultButtonSize(m_removeButton);
260: }
261: }
262:
263: class AddHandler extends SelectionAdapter {
264: public void widgetSelected(SelectionEvent e) {
265: m_layout.m_table.forceFocus();
266: IJavaProject javaProject = JavaCore.create(m_project);
267: int filter = IJavaSearchScope.SYSTEM_LIBRARIES;
268: IJavaElement[] elements = new IJavaElement[] { javaProject };
269: IJavaSearchScope scope = SearchEngine
270: .createJavaSearchScope(elements, filter);
271: int style = IJavaElementSearchConstants.CONSIDER_ALL_TYPES;
272: SelectionDialog dialog;
273: try {
274: dialog = JavaUI.createTypeDialog(getShell(), null,
275: scope, style, true);
276: } catch (JavaModelException jme) {
277: jme.printStackTrace();
278: return;
279: }
280: dialog.setTitle(CLASS_SELECT_TITLE);
281: dialog.setMessage(CLASS_SELECT_MESSAGE);
282: dialog.open();
283: Object[] items = dialog.getResult();
284: if (items != null) {
285: for (int i = 0; i < items.length; i++) {
286: internalAddBootClass(PatternHelper
287: .getFullyQualifiedName((IType) items[i]));
288: }
289: fireBootClassesChanged();
290: }
291: }
292: }
293:
294: class RemoveHandler extends SelectionAdapter {
295: public void widgetSelected(SelectionEvent e) {
296: m_layout.m_table.forceFocus();
297: int[] selection = m_layout.m_table.getSelectionIndices();
298:
299: for (int i = selection.length - 1; i >= 0; i--) {
300: ensureAdditionalBootClasses().removeInclude(
301: selection[i]);
302: }
303: m_layout.m_table.remove(selection);
304: testRemoveBootClasses();
305: }
306: }
307:
308: class TableSelectionListener extends SelectionAdapter {
309: public void widgetSelected(SelectionEvent e) {
310: m_layout.m_removeButton.setEnabled(true);
311: }
312: }
313:
314: class TableDataListener implements Listener {
315: public void handleEvent(Event e) {
316: TableItem item = (TableItem) e.item;
317: QualifiedClassName qcn = (QualifiedClassName) item
318: .getData();
319: qcn.setStringValue(item.getText(e.index));
320: fireBootClassesChanged();
321: }
322: }
323: }
|