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

Revision 1195, 3.9 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) 2005 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  */
19 package eu.moonlight3d.common.ui.propertysheet;
20
21 import java.util.ArrayList;
22
23 import org.w3c.dom.Element;
24 import org.w3c.dom.NodeList;
25
26 import com.trolltech.qt.gui.QCheckBox;
27 import com.trolltech.qt.gui.QHBoxLayout;
28 import com.trolltech.qt.gui.QWidget;
29
30 import eu.moonlight3d.framework.localization.L10N;
31 import eu.moonlight3d.framework.properties.BooleanValue;
32 import eu.moonlight3d.framework.properties.Property;
33 import eu.moonlight3d.framework.properties.PropertyContainer;
34
35 public class BooleanEditorFactory implements PropertyEditorFactory {
36         private class Parameters {
37                 public String labelText;
38         }
39        
40     public class BooleanValueEditor extends ValueEditor {
41         PropertySheet propertySheet;
42         Property property;
43         QCheckBox checkBox;
44        
45         BooleanValueEditor(Parameters parameters, PropertySheet sheet, QWidget parent, final Property property)
46         {
47             super(parent);
48             propertySheet=sheet;
49             this.property=property;
50            
51             checkBox=new QCheckBox(this);
52             if(parameters.labelText!=null) {
53                 checkBox.setText(parameters.labelText);
54             } else {
55                 checkBox.setText(property.getName());
56             }
57             checkBox.stateChanged.connect(this,"stateChanged()");
58            
59             QHBoxLayout layout=new QHBoxLayout();
60             layout.setMargin(0);
61             layout.addWidget(checkBox);
62             setLayout(layout);
63            
64             updateFromPropertyValue();
65         }
66        
67         public void stateChanged() {
68                 setPropertyValue(checkBox.isChecked());
69         }
70    
71         public void setPropertyValue(boolean value)
72         {
73                 try {
74                                 property.setValue(new BooleanValue(value));
75                         } catch (Exception e) {
76                                 // this cannot happen under normal circumstances
77                                 e.printStackTrace();
78                         }
79                 propertySheet.notifyPropertyUpdate(property);
80         }
81
82                 @Override
83                 public void updateFromPropertyValue() {
84                         try {
85                                 checkBox.setChecked((Boolean) property.getValue().getData());
86                         } catch (Exception e) {
87                                 // this cannot happen under normal circumstances.
88                                 e.printStackTrace();
89                         }
90                 }
91     }
92
93         public String getName() {
94                 return "BooleanEditor";
95         }
96
97         public Object parseExtraParameters(Element parameterRoot) throws Exception {
98                 Parameters parameters=new Parameters();
99                
100                 if(parameterRoot==null) {
101                         return parameters;
102                 }
103
104                 NodeList labelList=parameterRoot.getElementsByTagName("label");
105                 if(labelList.getLength()>1) {
106                         throw new Exception(L10N.translate("only one label can be defined for a boolean property editor"));
107                 } else if(labelList.getLength()==1) {
108                         Element labelElement=(Element) labelList.item(0);
109                         parameters.labelText=labelElement.getAttribute("text");
110                 }
111                
112                 return parameters;
113         }
114
115         public ValueEditor generatePropertyEditor(PropertySheet sheet, QWidget parent, PropertyContainer container, ArrayList<String> properties, Object additionalParams) {
116                 Property property=container.getProperty(properties.get(0));
117                 if(additionalParams==null) {
118                         additionalParams=new Parameters();
119                 }
120         return new BooleanValueEditor((Parameters) additionalParams,sheet,parent,property);
121         }
122 }
Note: See TracBrowser for help on using the browser.