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
 
  10  *    IBM - Initial API and implementation
 
  11  **********************************************************************/
 
  12 import java.util.List;
 
  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;
 
  43  * Dialog to manage the favorites list.
 
  45 public class OrganizeFavoritesDialog extends Dialog {
 
  46         protected List favorites = WebBrowserPreference.getInternalWebBrowserFavorites();
 
  48         public class FavoriteContentProvider implements IStructuredContentProvider {
 
  49                 public FavoriteContentProvider() {
 
  53                 public void dispose() { }
 
  55                 public Object[] getElements(Object inputElement) {
 
  56                         return favorites.toArray();
 
  59                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
 
  62         public class FavoriteLabelProvider implements ITableLabelProvider {
 
  63                 public FavoriteLabelProvider() {
 
  67                 public void addListener(ILabelProviderListener listener) { }
 
  69                 public void dispose() { }
 
  71                 public Image getColumnImage(Object element, int columnIndex) {
 
  73                                 return ImageResource.getImage(ImageResource.IMG_FAVORITE);
 
  77                 public String getColumnText(Object element, int columnIndex) {
 
  78                         Favorite favorite = (Favorite) element;
 
  80                                 return favorite.getName();
 
  82                                 return favorite.getURL();
 
  85                 public boolean isLabelProperty(Object element, String property) {
 
  89                 public void removeListener(ILabelProviderListener listener) { }
 
  93          * ManageFavoritesDialog constructor comment.
 
  94          * @param parentShell org.eclipse.swt.widgets.Shell
 
  97         public OrganizeFavoritesDialog(Shell parentShell) {
 
 100                 setBlockOnOpen(true);
 
 106         protected void configureShell(Shell newShell) {
 
 107                 super.configureShell(newShell);
 
 108                 newShell.setText(WebBrowserUIPlugin.getResource("%dialogOrganizeFavoritesTitle"));
 
 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);
 
 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);
 
 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);
 
 140                 TableLayout tableLayout = new TableLayout();
 
 141                 table.setLayout(tableLayout);
 
 142                 table.setHeaderVisible(true);
 
 144                 tableLayout.addColumnData(new ColumnWeightData(5, 50, true));
 
 145                 TableColumn col = new TableColumn(table, SWT.NONE);
 
 146                 col.setText(WebBrowserUIPlugin.getResource("%dialogOrganizeFavoritesName"));
 
 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);
 
 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"});
 
 159                 tableViewer.setCellEditors(new CellEditor[] {new TextCellEditor(table), new TextCellEditor(table)});
 
 161                 ICellModifier cellModifier = new ICellModifier() {
 
 162                         public Object getValue(Object element, String property) {
 
 163                                 Favorite f = (Favorite) element;
 
 164                                 if ("name".equals(property))
 
 170                         public boolean canModify(Object element, String property) {
 
 174                         public void modify(Object element, String property, Object value) {
 
 175                                 if (element instanceof Item)
 
 176                                         element = ((Item) element).getData();
 
 179                                         Favorite f = (Favorite) element;
 
 180                                         String s = (String) value;
 
 181                                         if ("name".equals(property))
 
 185                                         tableViewer.refresh(f);
 
 186                                 } catch (Exception ex) {
 
 187                                         ex.printStackTrace();
 
 191                 tableViewer.setCellModifier(cellModifier);
 
 193                 final Button remove = SWTUtil.createButton(composite, WebBrowserUIPlugin.getResource("%remove"));
 
 194                 remove.setEnabled(false);
 
 196                 tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
 
 197                         public void selectionChanged(SelectionChangedEvent event) {
 
 198                                 remove.setEnabled(!event.getSelection().isEmpty());
 
 202                 remove.addSelectionListener(new SelectionAdapter() {
 
 203                         public void widgetSelected(SelectionEvent e) {
 
 204                                 int index = table.getSelectionIndex();
 
 205                                 if (index < 0 || index >= favorites.size())
 
 208                                 tableViewer.remove(favorites.get(index));
 
 209                                 favorites.remove(index);
 
 213                 Dialog.applyDialogFont(composite);
 
 218         protected void okPressed() {
 
 219                 WebBrowserPreference.setInternalWebBrowserFavorites(favorites);