PreferenceItemPage.java
Go to the documentation of this file.
1 package com.generalrobotix.ui.grxui;
2 
3 
4 import java.util.ArrayList;
5 import java.util.Iterator;
6 
7 import org.eclipse.jface.dialogs.InputDialog;
8 import org.eclipse.jface.preference.PreferencePage;
9 import org.eclipse.jface.viewers.ArrayContentProvider;
10 import org.eclipse.jface.viewers.CellEditor;
11 import org.eclipse.jface.viewers.CheckboxCellEditor;
12 import org.eclipse.jface.viewers.ICellModifier;
13 import org.eclipse.jface.viewers.IStructuredSelection;
14 import org.eclipse.jface.viewers.ITableColorProvider;
15 import org.eclipse.jface.viewers.ITableLabelProvider;
16 import org.eclipse.jface.viewers.LabelProvider;
17 import org.eclipse.jface.viewers.TableViewer;
18 import org.eclipse.jface.viewers.TextCellEditor;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.events.SelectionListener;
22 import org.eclipse.swt.graphics.Color;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Table;
30 import org.eclipse.swt.widgets.TableColumn;
31 import org.eclipse.ui.IWorkbench;
32 import org.eclipse.ui.IWorkbenchPreferencePage;
33 
35 
36 public class PreferenceItemPage extends PreferencePage implements IWorkbenchPreferencePage {
37  TableViewer viewer_=null;
38  ArrayList<Item> input_=null;
39  String oldValue_=null;
40 
41  public PreferenceItemPage() {
42  super("");
43  setPreferenceStore(Activator.getDefault().getPreferenceStore());
44  }
45 
46  protected Control createContents(Composite parent) {
47  parent.setLayout(new GridLayout(2, false));
48 
49  viewer_ = new TableViewer(parent, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
50  viewer_.setContentProvider(new ArrayContentProvider());
51  viewer_.setLabelProvider(new ItemLabelProvider());
52  viewer_.setCellModifier(new CellModifier(viewer_));
53  Table table = viewer_.getTable();
54  String[] properties = new String[]{ "class","classPath","visible" };
55  viewer_.setColumnProperties(properties);
56  CellEditor[] editors = new CellEditor[]{
57  new TextCellEditor(table),
58  new TextCellEditor(table),
59  new CheckboxCellEditor(table)
60  };
61  viewer_.setCellEditors(editors);
62 
63  table.setLinesVisible(true);
64  table.setHeaderVisible(true);
65 
66  TableColumn column = new TableColumn(table, SWT.NONE);
67  column.setText(MessageBundle.get("PreferenceItemPage.table.column0"));
68  column = new TableColumn(table, SWT.NONE);
69  column.setText(MessageBundle.get("PreferenceItemPage.table.column1"));
70  column = new TableColumn(table,SWT.NONE);
71  column.setText(MessageBundle.get("PreferenceItemPage.table.column2"));
72  viewer_.setInput(input_);
73 
74  TableColumn[] columns = table.getColumns();
75  for(int i = 0; i < columns.length; i++) {
76  columns[i].pack();
77  }
78 
79  final Composite buttons = new Composite(parent, SWT.NONE);
80  GridData layoutData = new GridData();
81  layoutData.verticalAlignment = GridData.FILL;
82  buttons.setLayoutData(layoutData);
83  buttons.setLayout(new GridLayout());
84 
85  Button addButton = new Button(buttons, SWT.PUSH);
86  layoutData = new GridData();
87  layoutData.horizontalAlignment = GridData.FILL;
88  addButton.setLayoutData(layoutData);
89  addButton.setText(MessageBundle.get("PreferenceItemPage.button.add"));
90  addButton.addSelectionListener(new SelectionListener() {
91  public void widgetSelected(SelectionEvent e) {
92  InputDialog dialog = new InputDialog( buttons.getShell(), MessageBundle.get("PreferenceItemPage.dialog.title"),
93  MessageBundle.get("PreferenceItemPage.dialog.message"), null, null);
94  if(dialog.open()==InputDialog.OK){
95  String name = dialog.getValue();
96  if(!name.equals("")){
97  input_.add(new Item(name, "", "true", true));
98  viewer_.refresh();
99  }
100  }
101  }
102  public void widgetDefaultSelected(SelectionEvent e) {
103  }
104  });
105 
106  Button delButton = new Button(buttons, SWT.PUSH);
107  layoutData = new GridData();
108  layoutData.horizontalAlignment = GridData.FILL;
109  delButton.setLayoutData(layoutData);
110  delButton.setText(MessageBundle.get("PreferenceItemPage.button.delete"));
111  delButton.addSelectionListener(new SelectionListener() {
112  public void widgetSelected(SelectionEvent e) {
113  Item item = (Item) ((IStructuredSelection) viewer_.getSelection()).getFirstElement();
114  if(item.isUser_){
115  input_.remove(item);
116  viewer_.refresh();
117  }
118  }
119  public void widgetDefaultSelected(SelectionEvent e) {
120  }
121  });
122 
123  return viewer_.getControl();
124  }
125 
126  private void setItem(){
127  String itemList=getPreferenceStore().getString(PreferenceConstants.ITEM+"."+PreferenceConstants.CLASS);
128  String[] item=itemList.split(PreferenceConstants.SEPARATOR, -1);
129  String visibleList=getPreferenceStore().getString(PreferenceConstants.ITEM+"."+PreferenceConstants.VISIBLE);
130  String[] visible=visibleList.split(PreferenceConstants.SEPARATOR, -1);
131  for(int i=0; i<item.length; i++){
132  input_.add(new Item(item[i], "", visible[i], false));
133  }
134  itemList = getPreferenceStore().getString(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASS);
135  item=itemList.split(PreferenceConstants.SEPARATOR, -1);
136  String pathList=getPreferenceStore().getString(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASSPATH);
137  String[] path=pathList.split(PreferenceConstants.SEPARATOR, -1);
138  visibleList=getPreferenceStore().getString(PreferenceConstants.USERITEM+"."+PreferenceConstants.VISIBLE);
139  visible=visibleList.split(PreferenceConstants.SEPARATOR, -1);
140  for(int i=0; i<item.length; i++){
141  if(!item[i].equals(""))
142  input_.add(new Item(item[i], path[i], visible[i], true));
143  }
144  }
145 
146  public void init(IWorkbench workbench) {
147  input_ = new ArrayList<Item>();
148  setItem();
149  oldValue_=getPreferenceStore().getString(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASS);
150  }
151 
152  private class Item {
153  private String name_=null;
154  private String path_=null;
155  private boolean visible_=true;
156  private boolean isUser_=false;
157 
158  public Item(String name, String path, String visible, boolean isUser){
159  name_=name;
160  path_=path;
161  visible_=visible.equals("true")? true : false;
162  isUser_=isUser;
163  }
164  }
165 
166  private class ItemLabelProvider extends LabelProvider implements ITableLabelProvider, ITableColorProvider {
167  public Image getColumnImage(Object element, int columnIndex) {
168  return null;
169  }
170 
171  public String getColumnText(Object element, int columnIndex) {
172  Item item = (Item)element;
173  switch (columnIndex) {
174  case 0:
175  return item.name_;
176  case 1:
177  return item.path_;
178  case 2:
179  return item.visible_==true? "true" : "false";
180  default:
181  break;
182  }
183  return null;
184  }
185 
186  public Color getForeground(Object element, int columnIndex) {
187  Item item = (Item)element;
188  if(item.isUser_)
189  return null;
190  else
191  return Activator.getDefault().getColor("gray");
192  }
193 
194  public Color getBackground(Object element, int columnIndex) {
195  return null;
196  }
197  }
198 
199  private class CellModifier implements ICellModifier {
200  private TableViewer viewer;
201 
202  public CellModifier(TableViewer viewer) {
203  this.viewer = viewer;
204  }
205 
206  public boolean canModify(Object element, String property) {
207  Item item = (Item)element;
208  if(item.isUser_)
209  return true;
210  else
211  return false;
212  }
213 
214  public Object getValue(Object element, String property) {
215  Item item = (Item) element;
216  if (property == "class")
217  return item.name_;
218  if (property == "classPath")
219  return item.path_;
220  if (property == "visible")
221  return item.visible_;
222  return null;
223  }
224 
225  public void modify(Object element, String property, Object value) {
226  if (element instanceof org.eclipse.swt.widgets.Item) {
227  element = ((org.eclipse.swt.widgets.Item) element).getData();
228  }
229  Item item = (Item) element;
230  if(property == "class") {
231  item.name_=(String)value;
232  }
233  if(property == "classPath") {
234  item.path_=(String)value;
235  }
236  if(property == "visible") {
237  item.visible_=(Boolean)value;
238  }
239  viewer.update(element, null);
240  }
241  }
242 
243  public boolean performOk() {
244  String name="";
245  String path="";
246  String visible="";
247  Iterator<Item> it = input_.iterator();
248  while(it.hasNext()){
249  Item item=it.next();
250  if(item.isUser_){
251  name += item.name_;
252  path += item.path_;
253  visible += item.visible_? "true" : "false";
254  if(it.hasNext()){
257  visible += PreferenceConstants.SEPARATOR;
258  }
259  }
260  }
261  getPreferenceStore().putValue(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASSPATH, path);
262  getPreferenceStore().putValue(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASS, name);
263  getPreferenceStore().putValue(PreferenceConstants.USERITEM+"."+PreferenceConstants.VISIBLE, visible);
264  getPreferenceStore().firePropertyChangeEvent("userItemChange", oldValue_, name);
265  return true;
266  }
267 
268  protected void performDefaults() {
269  getPreferenceStore().setToDefault(PreferenceConstants.ITEM+"."+PreferenceConstants.CLASS);
270  getPreferenceStore().setToDefault(PreferenceConstants.ITEM+"."+PreferenceConstants.VISIBLE);
271  getPreferenceStore().setToDefault(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASS);
272  getPreferenceStore().setToDefault(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASSPATH);
273  getPreferenceStore().setToDefault(PreferenceConstants.USERITEM+"."+PreferenceConstants.VISIBLE);
274  input_.clear();
275  setItem();
276  viewer_.refresh();
277  }
278 
279  protected void performApply() {
280  performOk();
281  oldValue_=getPreferenceStore().getString(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASS);
282  }
283 }
static final String get(String key)
#define null
our own NULL pointer
Definition: IceTypes.h:57
void modify(Object element, String property, Object value)
png_infop png_charpp name
Definition: png.h:2382
png_voidp int value
Definition: png.h:2113
png_uint_32 i
Definition: png.h:2735
path
org
Item(String name, String path, String visible, boolean isUser)


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat May 8 2021 02:42:40