1 /*
2  * Created on 9 juil. 2003
3  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16  *
17  */
18 package org.gudy.azureus2.ui.swt.config;
19 
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.KeyAdapter;
22 import org.eclipse.swt.events.KeyEvent;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.widgets.*;
26 import org.gudy.azureus2.core3.config.COConfigurationManager;
27 import org.gudy.azureus2.core3.util.AERunnable;
28 import org.gudy.azureus2.ui.swt.Utils;
29 
30 /**
31  * @author Olivier
32  *
33  */
34 public class StringAreaParameter extends Parameter{
35 
36   private String name;
37   private Text inputField;
38   private String defaultValue;
39 
StringAreaParameter(Composite composite,final String name)40   public StringAreaParameter(Composite composite,final String name) {
41     this(composite, name, COConfigurationManager.getStringParameter(name));
42   }
43 
StringAreaParameter(Composite composite,final String name, String defaultValue )44   public StringAreaParameter(Composite composite,final String name, String defaultValue ) {
45   	super(name);
46     this.name = name;
47     this.defaultValue = defaultValue;
48     inputField = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.MULTI | SWT.V_SCROLL ) {
49   		// I know what I'm doing. Maybe ;)
50   		public void checkSubclass() {
51   		}
52 
53     	// @see org.eclipse.swt.widgets.Text#computeSize(int, int, boolean)
54     	public Point computeSize(int wHint, int hHint, boolean changed) {
55     		// Text widget, at least on Windows, forces the preferred width
56     		// to the width of the text inside of it
57     		// Fix this by forcing to LayoutData's minWidth
58     		if ( hHint==0 && !isVisible()){
59 
60     			return( new Point( 0, 0 ));
61     		}
62     		Point pt = super.computeSize(wHint, hHint, changed);
63 
64     		if (wHint == SWT.DEFAULT) {
65       		Object ld = getLayoutData();
66       		if (ld instanceof GridData) {
67       			if (((GridData)ld).grabExcessHorizontalSpace) {
68       				pt.x = 10;
69       			}
70       		}
71     		}
72 
73 
74     		return pt;
75     	}
76     };
77 
78     String value = COConfigurationManager.getStringParameter(name, defaultValue);
79     inputField.setText(value);
80     inputField.addListener(SWT.Verify, new Listener() {
81         public void handleEvent(Event e) {
82           e.doit = COConfigurationManager.verifyParameter(name, e.text );
83         }
84     });
85 
86     inputField.addListener(SWT.FocusOut, new Listener() {
87         public void handleEvent(Event event) {
88         	checkValue();
89         }
90     });
91 
92     inputField.addKeyListener(
93 			new KeyAdapter()
94 			{
95 				public void
96 				keyPressed(
97 					KeyEvent event )
98 				{
99 					int key = event.character;
100 
101 					if ( key <= 26 && key > 0 ){
102 
103 						key += 'a' - 1;
104 					}
105 
106 					if ( key == 'a' && event.stateMask == SWT.MOD1 ){
107 
108 						event.doit = false;
109 
110 						inputField.selectAll();
111 					}
112 				}
113 			});
114   }
115 
116   public int
getPreferredHeight( int line_count )117   getPreferredHeight(
118 	int	line_count )
119   {
120 	  return( inputField.getLineHeight() * line_count );
121   }
122 
123   protected void
checkValue()124   checkValue()
125   {
126 	  String	old_value = COConfigurationManager.getStringParameter( name, defaultValue );
127 	  String	new_value = inputField.getText();
128 
129 	  if ( !old_value.equals( new_value )){
130 	      COConfigurationManager.setParameter(name,new_value );
131 
132 	      if( change_listeners != null ) {
133 	        for (int i=0;i<change_listeners.size();i++){
134 	          ((ParameterChangeListener)change_listeners.get(i)).parameterChanged(StringAreaParameter.this,false);
135 	        }
136 	      }
137 	  }
138   }
139 
setLayoutData(Object layoutData)140   public void setLayoutData(Object layoutData) {
141   	Utils.adjustPXForDPI(layoutData);
142     inputField.setLayoutData(layoutData);
143   }
144 
setValue(final String value)145   public void setValue(final String value) {
146 		Utils.execSWTThread(new AERunnable() {
147 			public void runSupport() {
148 				if (inputField == null || inputField.isDisposed()
149 						|| inputField.getText().equals(value)) {
150 					return;
151 				}
152 				inputField.setText(value);
153 			}
154 		});
155 
156 		if (!COConfigurationManager.getStringParameter(name).equals(value)) {
157 			COConfigurationManager.setParameter(name, value);
158 		}
159 	}
160 
getValue()161   public String getValue() {
162     return inputField.getText();
163   }
164 
165   /* (non-Javadoc)
166    * @see org.gudy.azureus2.ui.swt.IParameter#getControl()
167    */
getControl()168   public Control getControl() {
169     return inputField;
170   }
171 
setValue(Object value)172   public void setValue(Object value) {
173   	if (value instanceof String) {
174   		setValue((String)value);
175   	}
176   }
177 
getValueObject()178   public Object getValueObject() {
179   	return COConfigurationManager.getStringParameter(name);
180   }
181 }
182