1 11 package org.eclipse.pde.internal.ui.editor.product; 12 13 import org.eclipse.core.resources.IFile; 14 import org.eclipse.core.resources.IResource; 15 import org.eclipse.core.resources.IWorkspaceRoot; 16 import org.eclipse.core.runtime.Path; 17 import org.eclipse.jface.dialogs.MessageDialog; 18 import org.eclipse.jface.window.Window; 19 import org.eclipse.pde.core.IModelChangedEvent; 20 import org.eclipse.pde.internal.core.iproduct.IConfigurationFileInfo; 21 import org.eclipse.pde.internal.core.iproduct.IProduct; 22 import org.eclipse.pde.internal.core.iproduct.IProductModel; 23 import org.eclipse.pde.internal.ui.PDEPlugin; 24 import org.eclipse.pde.internal.ui.PDEUIMessages; 25 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter; 26 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 27 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 28 import org.eclipse.pde.internal.ui.editor.PDESection; 29 import org.eclipse.pde.internal.ui.parts.FormEntry; 30 import org.eclipse.pde.internal.ui.util.FileNameFilter; 31 import org.eclipse.pde.internal.ui.util.FileValidator; 32 import org.eclipse.swt.SWT; 33 import org.eclipse.swt.dnd.Clipboard; 34 import org.eclipse.swt.events.SelectionAdapter; 35 import org.eclipse.swt.events.SelectionEvent; 36 import org.eclipse.swt.layout.GridData; 37 import org.eclipse.swt.widgets.Button; 38 import org.eclipse.swt.widgets.Composite; 39 import org.eclipse.swt.widgets.Control; 40 import org.eclipse.swt.widgets.Display; 41 import org.eclipse.swt.widgets.Text; 42 import org.eclipse.ui.IActionBars; 43 import org.eclipse.ui.PartInitException; 44 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; 45 import org.eclipse.ui.forms.events.HyperlinkEvent; 46 import org.eclipse.ui.forms.widgets.FormToolkit; 47 import org.eclipse.ui.forms.widgets.Section; 48 import org.eclipse.ui.ide.IDE; 49 import org.eclipse.ui.model.WorkbenchContentProvider; 50 import org.eclipse.ui.model.WorkbenchLabelProvider; 51 52 53 public class ConfigurationSection extends PDESection { 54 55 private Button fDefault; 56 private Button fCustom; 57 private FormEntry fCustomEntry; 58 59 public ConfigurationSection(PDEFormPage page, Composite parent) { 60 super(page, parent, Section.DESCRIPTION); 61 createClient(getSection(), page.getEditor().getToolkit()); 62 } 63 64 67 protected void createClient(Section section, FormToolkit toolkit) { 68 69 section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 70 GridData sectionData = new GridData(GridData.FILL_HORIZONTAL); 71 section.setLayoutData(sectionData); 72 73 section.setText(PDEUIMessages.ConfigurationSection_title); 74 section.setDescription(PDEUIMessages.ConfigurationSection_desc); 75 76 Composite client = toolkit.createComposite(section); 77 client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 3)); 78 client.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 79 80 fDefault = toolkit.createButton(client, PDEUIMessages.ConfigurationSection_default, SWT.RADIO); 81 GridData gd = new GridData(); 82 gd.horizontalSpan = 3; 83 fDefault.setLayoutData(gd); 84 fDefault.setEnabled(isEditable()); 85 fDefault.addSelectionListener(new SelectionAdapter() { 86 public void widgetSelected(SelectionEvent e) { 87 boolean selected = fDefault.getSelection(); 88 getConfigurationFileInfo().setUse(selected ? "default" : "custom"); fCustomEntry.setEditable(!selected); 90 } 91 }); 92 93 fCustom = toolkit.createButton(client, PDEUIMessages.ConfigurationSection_existing, SWT.RADIO); 94 gd = new GridData(); 95 gd.horizontalSpan = 3; 96 fCustom.setLayoutData(gd); 97 fCustom.setEnabled(isEditable()); 98 99 IActionBars actionBars = getPage().getPDEEditor().getEditorSite().getActionBars(); 100 fCustomEntry = new FormEntry(client, toolkit, PDEUIMessages.ConfigurationSection_file, PDEUIMessages.ConfigurationSection_browse, isEditable(), 35); fCustomEntry.setFormEntryListener(new FormEntryAdapter(this, actionBars) { 102 public void textValueChanged(FormEntry entry) { 103 getConfigurationFileInfo().setPath(entry.getValue()); 104 } 105 public void browseButtonSelected(FormEntry entry) { 106 handleBrowse(); 107 } 108 public void linkActivated(HyperlinkEvent e) { 109 handleOpen(); 110 } 111 }); 112 fCustomEntry.setEditable(isEditable()); 113 114 toolkit.paintBordersFor(client); 115 section.setClient(client); 116 getModel().addModelChangedListener(this); 118 } 119 120 123 public void dispose() { 124 IProductModel model = getModel(); 125 if (model != null) { 126 model.removeModelChangedListener(this); 127 } 128 super.dispose(); 129 } 130 131 private void handleBrowse() { 132 ElementTreeSelectionDialog dialog = 133 new ElementTreeSelectionDialog( 134 getSection().getShell(), 135 new WorkbenchLabelProvider(), 136 new WorkbenchContentProvider()); 137 138 dialog.setValidator(new FileValidator()); 139 dialog.setAllowMultiple(false); 140 dialog.setTitle(PDEUIMessages.ConfigurationSection_selection); 141 dialog.setMessage(PDEUIMessages.ConfigurationSection_message); 142 dialog.addFilter(new FileNameFilter("config.ini")); dialog.setInput(PDEPlugin.getWorkspace().getRoot()); 144 145 if (dialog.open() == Window.OK) { 146 IFile file = (IFile)dialog.getFirstResult(); 147 fCustomEntry.setValue(file.getFullPath().toString()); 148 } 149 } 150 151 public void refresh() { 152 IConfigurationFileInfo info = getConfigurationFileInfo(); 153 if (info == null) { 154 fDefault.setSelection(true); 155 fCustomEntry.setEditable(false); 156 } else { 157 boolean custom = "custom".equals(info.getUse()); fDefault.setSelection(!custom); 159 fCustom.setSelection(custom); 160 fCustomEntry.setValue(info.getPath(), true); 161 fCustomEntry.setEditable(custom); 162 } 163 super.refresh(); 164 } 165 166 private IConfigurationFileInfo getConfigurationFileInfo() { 167 IConfigurationFileInfo info = getProduct().getConfigurationFileInfo(); 168 if (info == null) { 169 info = getModel().getFactory().createConfigFileInfo(); 170 getProduct().setConfigurationFileInfo(info); 171 } 172 return info; 173 } 174 175 private IProduct getProduct() { 176 return getModel().getProduct(); 177 } 178 179 private IProductModel getModel() { 180 return (IProductModel)getPage().getPDEEditor().getAggregateModel(); 181 } 182 183 public void commit(boolean onSave) { 184 fCustomEntry.commit(); 185 super.commit(onSave); 186 } 187 188 public void cancelEdit() { 189 fCustomEntry.cancelEdit(); 190 super.cancelEdit(); 191 } 192 193 public boolean canPaste(Clipboard clipboard) { 194 Display d = getSection().getDisplay(); 195 Control c = d.getFocusControl(); 196 if (c instanceof Text) 197 return true; 198 return false; 199 } 200 201 private void handleOpen() { 202 IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot(); 203 Path path = new Path(fCustomEntry.getValue()); 204 if(path.isEmpty()){ 205 MessageDialog.openWarning(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.WindowImagesSection_open, PDEUIMessages.WindowImagesSection_emptyPath); return; 207 } 208 IResource resource = root.findMember(path); 209 try { 210 if (resource != null && resource instanceof IFile) 211 IDE.openEditor(PDEPlugin.getActivePage(), (IFile)resource, true); 212 else 213 MessageDialog.openWarning(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.WindowImagesSection_open, PDEUIMessages.WindowImagesSection_warning); } catch (PartInitException e) { 215 } 216 } 217 218 221 public void modelChanged(IModelChangedEvent e) { 222 if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 224 handleModelEventWorldChanged(e); 225 } 226 } 227 228 231 private void handleModelEventWorldChanged(IModelChangedEvent event) { 232 if (fCustomEntry.getText().isDisposed()) { 237 return; 238 } 239 refresh(); 241 getPage().setLastFocusControl(fCustomEntry.getText()); 250 } 251 } 252 | Popular Tags |