root/trunk/mlframework/src/eu/moonlight3d/common/ui/propertysheet/DirectoryListEditorFactory.java

Revision 1195, 5.7 kB (checked in by gregor, 11 months ago)

switched to a new implementation of the property system with plugable property types, which is known to break at least all property editors at the moment

Line 
1 /*
2  * Moonlight|3D Copyright (C) 2006 The Moonlight|3D team
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 2.1 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Created on Jul 28, 2006
19  */
20 package eu.moonlight3d.common.ui.propertysheet;
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24
25 import org.w3c.dom.Element;
26
27 import com.trolltech.qt.core.Qt;
28 import com.trolltech.qt.gui.QFileDialog;
29 import com.trolltech.qt.gui.QHBoxLayout;
30 import com.trolltech.qt.gui.QLabel;
31 import com.trolltech.qt.gui.QListWidget;
32 import com.trolltech.qt.gui.QListWidgetItem;
33 import com.trolltech.qt.gui.QPushButton;
34 import com.trolltech.qt.gui.QVBoxLayout;
35 import com.trolltech.qt.gui.QWidget;
36
37 import eu.moonlight3d.common.ui.propertysheet.IntegerMapFactory.Parameters;
38 import eu.moonlight3d.framework.properties.Property;
39 import eu.moonlight3d.framework.properties.PropertyContainer;
40 import eu.moonlight3d.framework.ui.ExceptionDialog;
41
42 public class DirectoryListEditorFactory implements PropertyEditorFactory {
43
44         private class DirectoryListEditor extends ValueEditor {
45                 private QListWidget directoryTable;
46                 private Property property;
47                 private PropertySheet propertySheet;
48                
49
50                 public DirectoryListEditor(Parameters parameters, PropertySheet sheet, QWidget parent, Property property) {
51                         super(parent);
52                         this.property=property;
53                         this.propertySheet=sheet;
54                        
55                         QLabel propertyLabel=new QLabel(this);
56                         propertyLabel.setText(property.getName());
57                        
58                         directoryTable=new QListWidget(this);
59                         directoryTable.setMinimumHeight(80);
60                        
61                         QPushButton add=new QPushButton(this);
62                         add.setText("Add");
63                         add.clicked.connect(this,"addEntry()");
64                        
65                         QPushButton edit=new QPushButton(this);                 
66                         edit.setText("Edit");
67                         edit.clicked.connect(this,"editEntry()");
68
69                         QPushButton delete=new QPushButton(this);
70                         delete.setText("Delete");
71                         delete.clicked.connect(this,"removeSelected()");
72
73                         QVBoxLayout layout=new QVBoxLayout();
74                         QHBoxLayout buttonLayout=new QHBoxLayout();
75                         buttonLayout.addWidget(add);
76                         buttonLayout.addStretch(1);
77                         buttonLayout.addWidget(edit);
78                         buttonLayout.addStretch(1);
79                         buttonLayout.addWidget(delete);
80                        
81                         layout.addWidget(propertyLabel);
82                         layout.addWidget(directoryTable,1);
83                         layout.addLayout(buttonLayout);
84                        
85                         setLayout(layout);
86                        
87                         updateFromPropertyValue();
88                 }
89                
90                 public void addEntry() {
91                         String directory=QFileDialog.getExistingDirectory(eu.moonlight3d.framework.State.getInstance().getMainWindow(),"Select plugin directory");
92                        
93                         if(directory==null) {
94                                 return;
95                         }
96                         try {
97                                 String[] directories=(String[]) property.getValue().getData();
98                                 directories=Arrays.copyOf(directories, directories.length+1);
99                                 property.getValue().setData(directories);
100                         } catch (Exception e) {
101                                 ExceptionDialog.run(e);
102                         }
103                         propertySheet.notifyPropertyUpdate(property);
104                         updateFromPropertyValue();
105                 }
106                
107                 public void editEntry() {
108                         if(directoryTable.currentItem()==null) {
109                                 return;
110                         }
111                        
112                         String directory=QFileDialog.getExistingDirectory(eu.moonlight3d.framework.State.getInstance().getMainWindow(),"Select plugin directory",directoryTable.currentItem().text());
113                         if(directory==null) {
114                                 return;
115                         }
116                         if(directory.equals("")) {
117                                 return;
118                         }
119
120                         try {
121                                 String[] directories=(String[]) property.getValue().getData();
122                                 directories[(Integer)directoryTable.currentItem().data(Qt.ItemDataRole.UserRole)]=directory;
123                                 property.getValue().setData(directories);
124                         } catch (Exception e) {
125                                 ExceptionDialog.run(e);
126                         }
127                         propertySheet.notifyPropertyUpdate(property);
128                         updateFromPropertyValue();
129                 }
130                
131                 public void removeSelected() {
132                         for(QListWidgetItem item : directoryTable.selectedItems()) {
133                                 int itemIndex=(Integer) item.data(Qt.ItemDataRole.UserRole);
134                                
135                                 try {
136                                         String[] directories=(String[]) property.getValue().getData();
137                                         for(int i=itemIndex;i<directories.length-1;i++) {
138                                                 directories[i]=directories[i+1];
139                                         }
140                                         directories=Arrays.copyOf(directories, directories.length-1);
141                                         property.getValue().setData(directories);                                       
142                                 } catch (Exception e) {
143                                         ExceptionDialog.run(e);
144                                 }
145                         }
146                         propertySheet.notifyPropertyUpdate(property);
147                         updateFromPropertyValue();
148                 }
149
150                 @Override
151                 public void updateFromPropertyValue() {
152                         directoryTable.clear();
153                         String[] directories;
154                         try {
155                                 directories = (String[]) property.getValue().getData();
156                                 for(int i=0;i<directories.length;i++) {
157                                         try {
158                                                 QListWidgetItem item=new QListWidgetItem(directoryTable);
159                                                 item.setText(directories[i]);
160                                                 item.setData(Qt.ItemDataRole.UserRole, i);
161                                         } catch (Exception e) {
162                                                 ExceptionDialog.run(e);
163                                         }
164                                 }
165                         } catch (Exception e) {
166                                 ExceptionDialog.run(e);
167                         }
168                 }
169
170         }
171        
172         public String getName() {
173                 return "DirectoryListEditor";
174         }
175
176         public Object parseExtraParameters(Element parameterRoot) throws Exception {
177                 return null;
178         }
179
180         public ValueEditor generatePropertyEditor(PropertySheet sheet, QWidget parent, PropertyContainer container,
181                         ArrayList<String> properties, Object additionalParams) {
182                 Property property=container.getProperty(properties.get(0));
183                 return new DirectoryListEditor((Parameters)additionalParams,sheet,parent,property);
184         }
185
186 }
Note: See TracBrowser for help on using the browser.