Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 7e1799f472c5cab28bb6e98fe8c05681a79bf00b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*******************************************************************************
 * Copyright (c) 2003, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.server.ui.internal.viewers;

import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.wst.server.core.IRuntime;
import org.eclipse.wst.server.core.IRuntimeWorkingCopy;
import org.eclipse.wst.server.core.internal.ResourceManager;
import org.eclipse.wst.server.ui.internal.Messages;
import org.eclipse.wst.server.ui.internal.Trace;
/**
 * 
 */
public class RuntimeComposite extends AbstractTableComposite {
	protected IRuntime selection;
	protected IRuntime defaultRuntime;
	protected RuntimeSelectionListener listener;
	
	public interface RuntimeSelectionListener {
		public void runtimeSelected(IRuntime runtime);
	}
	
	class RuntimeViewerSorter extends ViewerSorter {
		boolean sortByName;
		public RuntimeViewerSorter(boolean sortByName) {
			this.sortByName = sortByName;
		}
		
		public int compare(Viewer viewer, Object e1, Object e2) {
			IRuntime r1 = (IRuntime) e1;
			IRuntime r2 = (IRuntime) e2;
			if (sortByName)
				return collator.compare(notNull(r1.getName()), notNull(r2.getName()));
			
			if (r1.getRuntimeType() == null)
				return -1;
			if (r2.getRuntimeType() == null)
				return 1;
			return collator.compare(notNull(r1.getRuntimeType().getName()), notNull(r2.getRuntimeType().getName()));
		}
		
		protected String notNull(String s) {
			if (s == null)
				return "";
			return s;
		}
	}
	
	public RuntimeComposite(Composite parent, int style, RuntimeSelectionListener listener2) {
		super(parent, style);
		this.listener = listener2;
		
		TableLayout tableLayout = new TableLayout();
		table.setLayout(tableLayout);
		table.setHeaderVisible(true);

		tableLayout.addColumnData(new ColumnWeightData(60, 160, true));
		TableColumn col = new TableColumn(table, SWT.NONE);
		col.setText(Messages.columnName);
		col.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				tableViewer.setSorter(new RuntimeViewerSorter(true));
			}

			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}
		});

		tableLayout.addColumnData(new ColumnWeightData(45, 125, true));
		col = new TableColumn(table, SWT.NONE);
		col.setText(Messages.columnType);
		col.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				tableViewer.setSorter(new RuntimeViewerSorter(false));
			}

			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}
		});
		
		tableViewer.setContentProvider(new RuntimeContentProvider());
		tableViewer.setLabelProvider(new RuntimeTableLabelProvider());
		tableViewer.setInput(AbstractTreeContentProvider.ROOT);
		tableViewer.setColumnProperties(new String[] {"name", "type"});
		tableViewer.setSorter(new RuntimeViewerSorter(true));

		tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				Object obj = getSelection(event.getSelection());
				if (obj instanceof IRuntime)
					selection = (IRuntime) obj;
				else
					selection = null;
				listener.runtimeSelected(selection);
			}
		});
		
		table.addKeyListener(new KeyListener() {
			public void keyPressed(KeyEvent e) {
				if (e.character == 'l') {
					try {
						IRuntime runtime = getSelectedRuntime();
						IRuntimeWorkingCopy wc = runtime.createWorkingCopy();
						wc.setReadOnly(!runtime.isReadOnly());
						wc.save(false, null);
						refresh(runtime);
					} catch (Exception ex) {
						// ignore
					}
				}
			}

			public void keyReleased(KeyEvent e) {
				// do nothing
			}
		});
		
		final ResourceManager rm = ResourceManager.getInstance();
		defaultRuntime = rm.getDefaultRuntime();
		if (defaultRuntime != null)
			((CheckboxTableViewer)tableViewer).setChecked(defaultRuntime, true);

		((CheckboxTableViewer)tableViewer).addCheckStateListener(new ICheckStateListener() {
			public void checkStateChanged(CheckStateChangedEvent event) {
				try {
					IRuntime runtime = (IRuntime) event.getElement();
					if (event.getChecked()) {
						if (defaultRuntime != null && !runtime.equals(defaultRuntime))
							((CheckboxTableViewer)tableViewer).setChecked(defaultRuntime, false);
						rm.setDefaultRuntime(runtime);
						defaultRuntime = runtime;
					} else
						rm.setDefaultRuntime(null);
				} catch (Exception e) {
					Trace.trace(Trace.SEVERE, "Error setting default runtime", e);
				}
			}
		});
	}

	protected void createTable() {
		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE | SWT.CHECK);
	}

	protected void createTableViewer() {
		tableViewer = new LockedCheckboxTableViewer(table);
	}

	public IRuntime getSelectedRuntime() {
		return selection;
	}
}

Back to the top