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