Richard S. Hall | 85bafab | 2009-07-13 13:25:46 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | package org.cauldron.sigil.ui.preferences.installs; |
| 21 | |
| 22 | import java.util.HashMap; |
| 23 | import java.util.UUID; |
| 24 | |
| 25 | import org.cauldron.sigil.SigilCore; |
| 26 | import org.cauldron.sigil.install.IOSGiInstallType; |
| 27 | import org.cauldron.sigil.ui.SigilUI; |
| 28 | import org.cauldron.sigil.ui.preferences.ProjectDependentPreferencesPage; |
| 29 | import org.cauldron.sigil.ui.util.DefaultTableProvider; |
| 30 | import org.eclipse.jface.dialogs.MessageDialog; |
| 31 | import org.eclipse.jface.preference.IPreferenceStore; |
| 32 | import org.eclipse.jface.viewers.CheckStateChangedEvent; |
| 33 | import org.eclipse.jface.viewers.CheckboxTableViewer; |
| 34 | import org.eclipse.jface.viewers.ICheckStateListener; |
| 35 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
| 36 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 37 | import org.eclipse.jface.viewers.LabelProvider; |
| 38 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
| 39 | import org.eclipse.swt.SWT; |
| 40 | import org.eclipse.swt.events.SelectionAdapter; |
| 41 | import org.eclipse.swt.events.SelectionEvent; |
| 42 | import org.eclipse.swt.graphics.Image; |
| 43 | import org.eclipse.swt.layout.GridData; |
| 44 | import org.eclipse.swt.layout.GridLayout; |
| 45 | import org.eclipse.swt.widgets.Button; |
| 46 | import org.eclipse.swt.widgets.Composite; |
| 47 | import org.eclipse.swt.widgets.Control; |
| 48 | import org.eclipse.swt.widgets.DirectoryDialog; |
| 49 | import org.eclipse.swt.widgets.Label; |
| 50 | import org.eclipse.swt.widgets.Shell; |
| 51 | import org.eclipse.swt.widgets.Table; |
| 52 | import org.eclipse.ui.IWorkbench; |
| 53 | import org.eclipse.ui.IWorkbenchPreferencePage; |
| 54 | |
| 55 | public class OSGiInstallsPreferencePage extends ProjectDependentPreferencesPage implements |
| 56 | IWorkbenchPreferencePage { |
| 57 | |
| 58 | private class Install { |
| 59 | private String id; |
| 60 | private String location; |
| 61 | private IOSGiInstallType type; |
| 62 | |
| 63 | private Install(String id, String location) { |
| 64 | this.id = id; |
| 65 | this.location = location; |
| 66 | } |
| 67 | |
| 68 | private IOSGiInstallType getType() { |
| 69 | if ( type == null ) { |
| 70 | type = SigilCore.getInstallManager().findInstallType(location); |
| 71 | } |
| 72 | return type; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private HashMap<String, Install> installs = new HashMap<String, Install>(); |
| 77 | private CheckboxTableViewer viewer; |
| 78 | private boolean changed; |
| 79 | |
| 80 | public OSGiInstallsPreferencePage() { |
| 81 | super("OSGi Installs"); |
| 82 | } |
| 83 | |
| 84 | public void init(IWorkbench workbench) { |
| 85 | } |
| 86 | |
| 87 | |
| 88 | @Override |
| 89 | protected Control createContents(Composite parent) { |
| 90 | Composite control = new Composite(parent, SWT.NONE); |
| 91 | |
| 92 | buildComponents(control); |
| 93 | |
| 94 | load(); |
| 95 | |
| 96 | checkValid(); |
| 97 | |
| 98 | return control; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | protected boolean isDirty() { |
| 103 | return changed; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | private void buildComponents(Composite control) { |
| 108 | new Label(control, SWT.NONE).setText("Installs:"); |
| 109 | new Label(control, SWT.NONE); // padding |
| 110 | |
| 111 | Table table = new Table(control, SWT.CHECK | SWT.SINGLE | SWT.BORDER); |
| 112 | |
| 113 | Button add = new Button(control, SWT.PUSH); |
| 114 | add.setText("Add"); |
| 115 | add.addSelectionListener( new SelectionAdapter() { |
| 116 | @Override |
| 117 | public void widgetSelected(SelectionEvent e) { |
| 118 | add(); |
| 119 | } |
| 120 | }); |
| 121 | |
| 122 | final Button remove = new Button(control, SWT.PUSH); |
| 123 | remove.setEnabled(false); |
| 124 | remove.setText("Remove"); |
| 125 | remove.addSelectionListener( new SelectionAdapter() { |
| 126 | @Override |
| 127 | public void widgetSelected(SelectionEvent e) { |
| 128 | remove(); |
| 129 | } |
| 130 | }); |
| 131 | |
| 132 | // viewers |
| 133 | viewer = new CheckboxTableViewer(table); |
| 134 | viewer.setContentProvider( new DefaultTableProvider() { |
| 135 | public Object[] getElements(Object inputElement) { |
| 136 | return toArray(inputElement); |
| 137 | } |
| 138 | }); |
| 139 | |
| 140 | viewer.setLabelProvider( new LabelProvider() { |
| 141 | @Override |
| 142 | public String getText(Object element) { |
| 143 | Install i = (Install) element; |
| 144 | IOSGiInstallType type = i.getType(); |
| 145 | if ( type == null ) { |
| 146 | return "<invalid> [" + i.location + "]"; |
| 147 | } |
| 148 | else { |
| 149 | return type.getName() + " " + type.getVersion() + " [" + i.location + "]"; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public Image getImage(Object element) { |
| 155 | Install i = (Install) element; |
| 156 | IOSGiInstallType type = i.getType(); |
| 157 | |
| 158 | if (type == null) { |
| 159 | return null; |
| 160 | } else { |
| 161 | return type.getIcon(); |
| 162 | } |
| 163 | } |
| 164 | }); |
| 165 | |
| 166 | viewer.addSelectionChangedListener( new ISelectionChangedListener() { |
| 167 | public void selectionChanged(SelectionChangedEvent event) { |
| 168 | boolean enabled = !event.getSelection().isEmpty(); |
| 169 | remove.setEnabled(enabled ); |
| 170 | } |
| 171 | }); |
| 172 | |
| 173 | viewer.addCheckStateListener( new ICheckStateListener () { |
| 174 | public void checkStateChanged(CheckStateChangedEvent event) { |
| 175 | if ( event.getChecked() ) { |
| 176 | changed = true; |
| 177 | } |
| 178 | viewer.setCheckedElements( new Object[] { event.getElement() } ); |
| 179 | } |
| 180 | }); |
| 181 | |
| 182 | viewer.setInput(installs.values()); |
| 183 | |
| 184 | // layout |
| 185 | control.setLayout( new GridLayout(2, false) ); |
| 186 | table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 3)); |
| 187 | add.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); |
| 188 | remove.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); |
| 189 | } |
| 190 | |
| 191 | private void load() { |
| 192 | String pref = getPreferenceStore().getString(SigilCore.OSGI_INSTALLS); |
| 193 | if ( pref != null && pref.length() > 0 ) { |
| 194 | for ( String id : pref.split(",") ) { |
| 195 | String loc = getPreferenceStore().getString( SigilCore.OSGI_INSTALL_PREFIX + id ); |
| 196 | installs.put( id, new Install( id, loc ) ); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | viewer.refresh(); |
| 201 | |
| 202 | if ( !installs.isEmpty() ) { |
| 203 | String defId = getPreferenceStore().getString( SigilCore.OSGI_DEFAULT_INSTALL_ID ); |
| 204 | if ( defId == null || defId.trim().length() == 0 ) { |
| 205 | viewer.setCheckedElements( new Object[] { installs.values().iterator().next() } ); |
| 206 | } |
| 207 | else { |
| 208 | viewer.setCheckedElements( new Object[] { installs.get( defId ) } ); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | protected void doSave() { |
| 214 | // zero out old configs |
| 215 | String pref = getPreferenceStore().getString(SigilCore.OSGI_INSTALLS); |
| 216 | if ( pref != null && pref.length() > 0 ) { |
| 217 | for ( String id : pref.split(",") ) { |
| 218 | getPreferenceStore().setToDefault( SigilCore.OSGI_INSTALL_PREFIX + id ); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // store new configs |
| 223 | if ( installs.isEmpty() ) { |
| 224 | getPreferenceStore().setToDefault(SigilCore.OSGI_INSTALLS); |
| 225 | getPreferenceStore().setToDefault(SigilCore.OSGI_DEFAULT_INSTALL_ID); |
| 226 | } |
| 227 | else { |
| 228 | StringBuffer buf = new StringBuffer(); |
| 229 | for (Install i : installs.values() ) { |
| 230 | if ( buf.length() > 0 ) { |
| 231 | buf.append(","); |
| 232 | } |
| 233 | buf.append( i.id ); |
| 234 | getPreferenceStore().setValue( SigilCore.OSGI_INSTALL_PREFIX + i.id, i.location ); |
| 235 | } |
| 236 | |
| 237 | getPreferenceStore().setValue( SigilCore.OSGI_INSTALLS, buf.toString() ); |
| 238 | Install def = (Install) viewer.getCheckedElements()[0]; |
| 239 | getPreferenceStore().setValue(SigilCore.OSGI_DEFAULT_INSTALL_ID, def.id); |
| 240 | } |
| 241 | changed = false; |
| 242 | } |
| 243 | |
| 244 | private boolean isOK() { |
| 245 | return installs.isEmpty() || viewer.getCheckedElements().length > 0; |
| 246 | } |
| 247 | |
| 248 | private void add() { |
| 249 | Shell shell = SigilUI.getActiveWorkbenchShell(); |
| 250 | DirectoryDialog dialog = new DirectoryDialog(shell); |
| 251 | String dir = dialog.open(); |
| 252 | if ( dir != null ) { |
| 253 | Install install = new Install( UUID.randomUUID().toString(), dir ); |
| 254 | if ( install.getType() == null ) { |
| 255 | MessageDialog.openError(shell, "Error", "Invalid OSGi install directory" ); |
| 256 | } |
| 257 | else { |
| 258 | boolean empty = installs.isEmpty(); |
| 259 | |
| 260 | installs.put( install.id, install ); |
| 261 | viewer.refresh(); |
| 262 | |
| 263 | if ( empty ) { |
| 264 | viewer.setCheckedElements( new Object[] { install } ); |
| 265 | } |
| 266 | |
| 267 | checkValid(); |
| 268 | changed = true; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | private void checkValid() { |
| 274 | if ( isOK() ) { |
| 275 | setErrorMessage(null); |
| 276 | setValid(true); |
| 277 | } |
| 278 | else { |
| 279 | setErrorMessage("Missing default OSGi install"); |
| 280 | setValid(false); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | private void remove() { |
| 285 | IStructuredSelection sel = (IStructuredSelection) viewer.getSelection(); |
| 286 | Install i = (Install) sel.getFirstElement(); |
| 287 | boolean def = viewer.getChecked(i); |
| 288 | installs.remove(i.id); |
| 289 | viewer.refresh(); |
| 290 | if ( def && installs.size() > 0 ) { |
| 291 | viewer.setCheckedElements( new Object[] { installs.values().iterator().next() } ); |
| 292 | } |
| 293 | checkValid(); |
| 294 | changed = true; |
| 295 | } |
| 296 | |
| 297 | @Override |
| 298 | protected IPreferenceStore doGetPreferenceStore() { |
| 299 | return SigilCore.getDefault().getPreferenceStore(); |
| 300 | } |
| 301 | } |