00001 package com.generalrobotix.ui.grxui; 00002 00003 00004 import java.util.ArrayList; 00005 import java.util.Iterator; 00006 00007 import org.eclipse.jface.dialogs.InputDialog; 00008 import org.eclipse.jface.preference.PreferencePage; 00009 import org.eclipse.jface.viewers.ArrayContentProvider; 00010 import org.eclipse.jface.viewers.CellEditor; 00011 import org.eclipse.jface.viewers.CheckboxCellEditor; 00012 import org.eclipse.jface.viewers.ICellModifier; 00013 import org.eclipse.jface.viewers.IStructuredSelection; 00014 import org.eclipse.jface.viewers.ITableColorProvider; 00015 import org.eclipse.jface.viewers.ITableLabelProvider; 00016 import org.eclipse.jface.viewers.LabelProvider; 00017 import org.eclipse.jface.viewers.TableViewer; 00018 import org.eclipse.jface.viewers.TextCellEditor; 00019 import org.eclipse.swt.SWT; 00020 import org.eclipse.swt.events.SelectionEvent; 00021 import org.eclipse.swt.events.SelectionListener; 00022 import org.eclipse.swt.graphics.Color; 00023 import org.eclipse.swt.graphics.Image; 00024 import org.eclipse.swt.layout.GridData; 00025 import org.eclipse.swt.layout.GridLayout; 00026 import org.eclipse.swt.widgets.Button; 00027 import org.eclipse.swt.widgets.Composite; 00028 import org.eclipse.swt.widgets.Control; 00029 import org.eclipse.swt.widgets.Table; 00030 import org.eclipse.swt.widgets.TableColumn; 00031 import org.eclipse.ui.IWorkbench; 00032 import org.eclipse.ui.IWorkbenchPreferencePage; 00033 00034 import com.generalrobotix.ui.util.MessageBundle; 00035 00036 public class PreferenceItemPage extends PreferencePage implements IWorkbenchPreferencePage { 00037 TableViewer viewer_=null; 00038 ArrayList<Item> input_=null; 00039 String oldValue_=null; 00040 00041 public PreferenceItemPage() { 00042 super(""); 00043 setPreferenceStore(Activator.getDefault().getPreferenceStore()); 00044 } 00045 00046 protected Control createContents(Composite parent) { 00047 parent.setLayout(new GridLayout(2, false)); 00048 00049 viewer_ = new TableViewer(parent, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION); 00050 viewer_.setContentProvider(new ArrayContentProvider()); 00051 viewer_.setLabelProvider(new ItemLabelProvider()); 00052 viewer_.setCellModifier(new CellModifier(viewer_)); 00053 Table table = viewer_.getTable(); 00054 String[] properties = new String[]{ "class","classPath","visible" }; 00055 viewer_.setColumnProperties(properties); 00056 CellEditor[] editors = new CellEditor[]{ 00057 new TextCellEditor(table), 00058 new TextCellEditor(table), 00059 new CheckboxCellEditor(table) 00060 }; 00061 viewer_.setCellEditors(editors); 00062 00063 table.setLinesVisible(true); 00064 table.setHeaderVisible(true); 00065 00066 TableColumn column = new TableColumn(table, SWT.NONE); 00067 column.setText(MessageBundle.get("PreferenceItemPage.table.column0")); 00068 column = new TableColumn(table, SWT.NONE); 00069 column.setText(MessageBundle.get("PreferenceItemPage.table.column1")); 00070 column = new TableColumn(table,SWT.NONE); 00071 column.setText(MessageBundle.get("PreferenceItemPage.table.column2")); 00072 viewer_.setInput(input_); 00073 00074 TableColumn[] columns = table.getColumns(); 00075 for(int i = 0; i < columns.length; i++) { 00076 columns[i].pack(); 00077 } 00078 00079 final Composite buttons = new Composite(parent, SWT.NONE); 00080 GridData layoutData = new GridData(); 00081 layoutData.verticalAlignment = GridData.FILL; 00082 buttons.setLayoutData(layoutData); 00083 buttons.setLayout(new GridLayout()); 00084 00085 Button addButton = new Button(buttons, SWT.PUSH); 00086 layoutData = new GridData(); 00087 layoutData.horizontalAlignment = GridData.FILL; 00088 addButton.setLayoutData(layoutData); 00089 addButton.setText(MessageBundle.get("PreferenceItemPage.button.add")); 00090 addButton.addSelectionListener(new SelectionListener() { 00091 public void widgetSelected(SelectionEvent e) { 00092 InputDialog dialog = new InputDialog( buttons.getShell(), MessageBundle.get("PreferenceItemPage.dialog.title"), 00093 MessageBundle.get("PreferenceItemPage.dialog.message"), null, null); 00094 if(dialog.open()==InputDialog.OK){ 00095 String name = dialog.getValue(); 00096 if(!name.equals("")){ 00097 input_.add(new Item(name, "", "true", true)); 00098 viewer_.refresh(); 00099 } 00100 } 00101 } 00102 public void widgetDefaultSelected(SelectionEvent e) { 00103 } 00104 }); 00105 00106 Button delButton = new Button(buttons, SWT.PUSH); 00107 layoutData = new GridData(); 00108 layoutData.horizontalAlignment = GridData.FILL; 00109 delButton.setLayoutData(layoutData); 00110 delButton.setText(MessageBundle.get("PreferenceItemPage.button.delete")); 00111 delButton.addSelectionListener(new SelectionListener() { 00112 public void widgetSelected(SelectionEvent e) { 00113 Item item = (Item) ((IStructuredSelection) viewer_.getSelection()).getFirstElement(); 00114 if(item.isUser_){ 00115 input_.remove(item); 00116 viewer_.refresh(); 00117 } 00118 } 00119 public void widgetDefaultSelected(SelectionEvent e) { 00120 } 00121 }); 00122 00123 return viewer_.getControl(); 00124 } 00125 00126 private void setItem(){ 00127 String itemList=getPreferenceStore().getString(PreferenceConstants.ITEM+"."+PreferenceConstants.CLASS); 00128 String[] item=itemList.split(PreferenceConstants.SEPARATOR, -1); 00129 String visibleList=getPreferenceStore().getString(PreferenceConstants.ITEM+"."+PreferenceConstants.VISIBLE); 00130 String[] visible=visibleList.split(PreferenceConstants.SEPARATOR, -1); 00131 for(int i=0; i<item.length; i++){ 00132 input_.add(new Item(item[i], "", visible[i], false)); 00133 } 00134 itemList = getPreferenceStore().getString(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASS); 00135 item=itemList.split(PreferenceConstants.SEPARATOR, -1); 00136 String pathList=getPreferenceStore().getString(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASSPATH); 00137 String[] path=pathList.split(PreferenceConstants.SEPARATOR, -1); 00138 visibleList=getPreferenceStore().getString(PreferenceConstants.USERITEM+"."+PreferenceConstants.VISIBLE); 00139 visible=visibleList.split(PreferenceConstants.SEPARATOR, -1); 00140 for(int i=0; i<item.length; i++){ 00141 if(!item[i].equals("")) 00142 input_.add(new Item(item[i], path[i], visible[i], true)); 00143 } 00144 } 00145 00146 public void init(IWorkbench workbench) { 00147 input_ = new ArrayList<Item>(); 00148 setItem(); 00149 oldValue_=getPreferenceStore().getString(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASS); 00150 } 00151 00152 private class Item { 00153 private String name_=null; 00154 private String path_=null; 00155 private boolean visible_=true; 00156 private boolean isUser_=false; 00157 00158 public Item(String name, String path, String visible, boolean isUser){ 00159 name_=name; 00160 path_=path; 00161 visible_=visible.equals("true")? true : false; 00162 isUser_=isUser; 00163 } 00164 } 00165 00166 private class ItemLabelProvider extends LabelProvider implements ITableLabelProvider, ITableColorProvider { 00167 public Image getColumnImage(Object element, int columnIndex) { 00168 return null; 00169 } 00170 00171 public String getColumnText(Object element, int columnIndex) { 00172 Item item = (Item)element; 00173 switch (columnIndex) { 00174 case 0: 00175 return item.name_; 00176 case 1: 00177 return item.path_; 00178 case 2: 00179 return item.visible_==true? "true" : "false"; 00180 default: 00181 break; 00182 } 00183 return null; 00184 } 00185 00186 public Color getForeground(Object element, int columnIndex) { 00187 Item item = (Item)element; 00188 if(item.isUser_) 00189 return null; 00190 else 00191 return Activator.getDefault().getColor("gray"); 00192 } 00193 00194 public Color getBackground(Object element, int columnIndex) { 00195 return null; 00196 } 00197 } 00198 00199 private class CellModifier implements ICellModifier { 00200 private TableViewer viewer; 00201 00202 public CellModifier(TableViewer viewer) { 00203 this.viewer = viewer; 00204 } 00205 00206 public boolean canModify(Object element, String property) { 00207 Item item = (Item)element; 00208 if(item.isUser_) 00209 return true; 00210 else 00211 return false; 00212 } 00213 00214 public Object getValue(Object element, String property) { 00215 Item item = (Item) element; 00216 if (property == "class") 00217 return item.name_; 00218 if (property == "classPath") 00219 return item.path_; 00220 if (property == "visible") 00221 return item.visible_; 00222 return null; 00223 } 00224 00225 public void modify(Object element, String property, Object value) { 00226 if (element instanceof org.eclipse.swt.widgets.Item) { 00227 element = ((org.eclipse.swt.widgets.Item) element).getData(); 00228 } 00229 Item item = (Item) element; 00230 if(property == "class") { 00231 item.name_=(String)value; 00232 } 00233 if(property == "classPath") { 00234 item.path_=(String)value; 00235 } 00236 if(property == "visible") { 00237 item.visible_=(Boolean)value; 00238 } 00239 viewer.update(element, null); 00240 } 00241 } 00242 00243 public boolean performOk() { 00244 String name=""; 00245 String path=""; 00246 String visible=""; 00247 Iterator<Item> it = input_.iterator(); 00248 while(it.hasNext()){ 00249 Item item=it.next(); 00250 if(item.isUser_){ 00251 name += item.name_; 00252 path += item.path_; 00253 visible += item.visible_? "true" : "false"; 00254 if(it.hasNext()){ 00255 name += PreferenceConstants.SEPARATOR; 00256 path += PreferenceConstants.SEPARATOR; 00257 visible += PreferenceConstants.SEPARATOR; 00258 } 00259 } 00260 } 00261 getPreferenceStore().putValue(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASSPATH, path); 00262 getPreferenceStore().putValue(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASS, name); 00263 getPreferenceStore().putValue(PreferenceConstants.USERITEM+"."+PreferenceConstants.VISIBLE, visible); 00264 getPreferenceStore().firePropertyChangeEvent("userItemChange", oldValue_, name); 00265 return true; 00266 } 00267 00268 protected void performDefaults() { 00269 getPreferenceStore().setToDefault(PreferenceConstants.ITEM+"."+PreferenceConstants.CLASS); 00270 getPreferenceStore().setToDefault(PreferenceConstants.ITEM+"."+PreferenceConstants.VISIBLE); 00271 getPreferenceStore().setToDefault(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASS); 00272 getPreferenceStore().setToDefault(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASSPATH); 00273 getPreferenceStore().setToDefault(PreferenceConstants.USERITEM+"."+PreferenceConstants.VISIBLE); 00274 input_.clear(); 00275 setItem(); 00276 viewer_.refresh(); 00277 } 00278 00279 protected void performApply() { 00280 performOk(); 00281 oldValue_=getPreferenceStore().getString(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASS); 00282 } 00283 }