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