Refactory: wizard plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / net / sourceforge / phpeclipse / webbrowser / internal / OrganizeFavoritesDialog.java
1 package net.sourceforge.phpeclipse.webbrowser.internal;
2
3 /**********************************************************************
4  * Copyright (c) 2003 IBM Corporation and others.
5  * All rights reserved. � This program and the accompanying materials
6  * are made available under the terms of the Common Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/cpl-v10.html
9  �*
10  * Contributors:
11  *    IBM - Initial API and implementation
12  **********************************************************************/
13 import java.util.List;
14
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.jface.viewers.CellEditor;
18 import org.eclipse.jface.viewers.ColumnWeightData;
19 import org.eclipse.jface.viewers.ICellModifier;
20 import org.eclipse.jface.viewers.ILabelProviderListener;
21 import org.eclipse.jface.viewers.ISelectionChangedListener;
22 import org.eclipse.jface.viewers.IStructuredContentProvider;
23 import org.eclipse.jface.viewers.ITableLabelProvider;
24 import org.eclipse.jface.viewers.SelectionChangedEvent;
25 import org.eclipse.jface.viewers.TableLayout;
26 import org.eclipse.jface.viewers.TableViewer;
27 import org.eclipse.jface.viewers.TextCellEditor;
28 import org.eclipse.jface.viewers.Viewer;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.SelectionAdapter;
31 import org.eclipse.swt.events.SelectionEvent;
32 import org.eclipse.swt.graphics.Image;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.Item;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.Shell;
41 import org.eclipse.swt.widgets.Table;
42 import org.eclipse.swt.widgets.TableColumn;
43
44 /**
45  * Dialog to manage the favorites list.
46  */
47 public class OrganizeFavoritesDialog extends Dialog {
48         protected List favorites = WebBrowserPreference
49                         .getInternalWebBrowserFavorites();
50
51         public class FavoriteContentProvider implements IStructuredContentProvider {
52                 public FavoriteContentProvider() {
53                         super();
54                 }
55
56                 public void dispose() {
57                 }
58
59                 public Object[] getElements(Object inputElement) {
60                         return favorites.toArray();
61                 }
62
63                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
64                 }
65         }
66
67         public class FavoriteLabelProvider implements ITableLabelProvider {
68                 public FavoriteLabelProvider() {
69                         super();
70                 }
71
72                 public void addListener(ILabelProviderListener listener) {
73                 }
74
75                 public void dispose() {
76                 }
77
78                 public Image getColumnImage(Object element, int columnIndex) {
79                         if (columnIndex == 0)
80                                 return ImageResource.getImage(ImageResource.IMG_FAVORITE);
81                         return null;
82                 }
83
84                 public String getColumnText(Object element, int columnIndex) {
85                         Favorite favorite = (Favorite) element;
86                         if (columnIndex == 0)
87                                 return favorite.getName();
88                         else
89                                 return favorite.getURL();
90                 }
91
92                 public boolean isLabelProperty(Object element, String property) {
93                         return false;
94                 }
95
96                 public void removeListener(ILabelProviderListener listener) {
97                 }
98         }
99
100         /**
101          * ManageFavoritesDialog constructor comment.
102          * 
103          * @param parentShell
104          * org.eclipse.swt.widgets.Shell @
105          */
106         public OrganizeFavoritesDialog(Shell parentShell) {
107                 super(parentShell);
108
109                 setBlockOnOpen(true);
110         }
111
112         /**
113          * 
114          */
115         protected void configureShell(Shell newShell) {
116                 super.configureShell(newShell);
117                 newShell.setText(WebBrowserUIPlugin
118                                 .getResource("%dialogOrganizeFavoritesTitle"));
119         }
120
121         /**
122          * 
123          */
124         protected Control createDialogArea(Composite parent) {
125                 Composite composite = new Composite(parent, SWT.NONE);
126                 GridLayout layout = new GridLayout();
127                 layout.numColumns = 2;
128                 layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
129                 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
130                 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
131                 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
132                 composite.setLayout(layout);
133                 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
134                 composite.setFont(parent.getFont());
135                 // WorkbenchHelp.setHelp(composite, ContextIds.TERMINATE_SERVER_DIALOG);
136
137                 Label label = new Label(composite, SWT.NONE);
138                 label.setText(WebBrowserUIPlugin
139                                 .getResource("%dialogOrganizeFavoritesMessage"));
140                 GridData data = new GridData();
141                 data.horizontalSpan = 2;
142                 label.setLayoutData(data);
143
144                 final Table table = new Table(composite, SWT.BORDER
145                                 | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE);
146                 data = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
147                 data.widthHint = 300;
148                 data.heightHint = 150;
149                 table.setLayoutData(data);
150                 table.setLinesVisible(true);
151
152                 TableLayout tableLayout = new TableLayout();
153                 table.setLayout(tableLayout);
154                 table.setHeaderVisible(true);
155
156                 tableLayout.addColumnData(new ColumnWeightData(5, 50, true));
157                 TableColumn col = new TableColumn(table, SWT.NONE);
158                 col.setText(WebBrowserUIPlugin
159                                 .getResource("%dialogOrganizeFavoritesName"));
160
161                 tableLayout.addColumnData(new ColumnWeightData(6, 60, true));
162                 col = new TableColumn(table, SWT.NONE);
163                 col.setText(WebBrowserUIPlugin
164                                 .getResource("%dialogOrganizeFavoritesURL"));
165                 table.setLayout(tableLayout);
166
167                 final TableViewer tableViewer = new TableViewer(table);
168                 tableViewer.setContentProvider(new FavoriteContentProvider());
169                 tableViewer.setLabelProvider(new FavoriteLabelProvider());
170                 tableViewer.setInput("root");
171                 tableViewer.setColumnProperties(new String[] { "name", "url" });
172
173                 tableViewer.setCellEditors(new CellEditor[] {
174                                 new TextCellEditor(table), new TextCellEditor(table) });
175
176                 ICellModifier cellModifier = new ICellModifier() {
177                         public Object getValue(Object element, String property) {
178                                 Favorite f = (Favorite) element;
179                                 if ("name".equals(property))
180                                         return f.getName();
181                                 else
182                                         return f.getURL();
183                         }
184
185                         public boolean canModify(Object element, String property) {
186                                 return true;
187                         }
188
189                         public void modify(Object element, String property, Object value) {
190                                 if (element instanceof Item)
191                                         element = ((Item) element).getData();
192
193                                 try {
194                                         Favorite f = (Favorite) element;
195                                         String s = (String) value;
196                                         if ("name".equals(property))
197                                                 f.setName(s);
198                                         else
199                                                 f.setURL(s);
200                                         tableViewer.refresh(f);
201                                 } catch (Exception ex) {
202                                         ex.printStackTrace();
203                                 }
204                         }
205                 };
206                 tableViewer.setCellModifier(cellModifier);
207
208                 final Button remove = SWTUtil.createButton(composite,
209                                 WebBrowserUIPlugin.getResource("%remove"));
210                 remove.setEnabled(false);
211
212                 tableViewer
213                                 .addSelectionChangedListener(new ISelectionChangedListener() {
214                                         public void selectionChanged(SelectionChangedEvent event) {
215                                                 remove.setEnabled(!event.getSelection().isEmpty());
216                                         }
217                                 });
218
219                 remove.addSelectionListener(new SelectionAdapter() {
220                         public void widgetSelected(SelectionEvent e) {
221                                 int index = table.getSelectionIndex();
222                                 if (index < 0 || index >= favorites.size())
223                                         return;
224
225                                 tableViewer.remove(favorites.get(index));
226                                 favorites.remove(index);
227                         }
228                 });
229
230                 Dialog.applyDialogFont(composite);
231
232                 return composite;
233         }
234
235         protected void okPressed() {
236                 WebBrowserPreference.setInternalWebBrowserFavorites(favorites);
237                 super.okPressed();
238         }
239 }