1 /*
2  * Created on 02-Oct-2005
3  * Created by Paul Gardner
4  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  */
19 
20 package org.gudy.azureus2.ui.swt;
21 
22 import java.util.HashMap;
23 import java.util.Map;
24 
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.custom.ScrolledComposite;
27 import org.eclipse.swt.events.ControlAdapter;
28 import org.eclipse.swt.events.ControlEvent;
29 import org.eclipse.swt.graphics.Rectangle;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.*;
33 import org.gudy.azureus2.core3.internat.MessageText;
34 import org.gudy.azureus2.ui.swt.components.BufferedLabel;
35 import org.gudy.azureus2.ui.swt.components.shell.ShellFactory;
36 
37 
38 public class
39 PropertiesWindow
40 {
41 	private final Shell		shell;
42 
43 	private Map<String,BufferedLabel>	field_map = new HashMap<String, BufferedLabel>();
44 
45 	public
PropertiesWindow( String object_name, String[] keys, String[] values )46 	PropertiesWindow(
47 		String		object_name,
48 		String[]	keys,
49 		String[]	values )
50 	{
51 		shell = ShellFactory.createMainShell(SWT.TITLE | SWT.CLOSE |SWT.RESIZE );
52 
53 		shell.setText( MessageText.getString( "props.window.title", new String[]{ object_name }));
54 
55 		Utils.setShellIcon(shell);
56 
57 	    GridLayout layout = new GridLayout();
58 	    layout.numColumns = 3;
59 	    shell.setLayout(layout);
60 
61 	    final ScrolledComposite scrollable = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL );
62 	    GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
63 	    gridData.horizontalSpan = 3;
64 
65 		Utils.setLayoutData(scrollable,  gridData );
66 
67 		/*
68 		 * Main content composite where panels will be created
69 		 */
70 		final Composite main = new Composite(scrollable, SWT.NONE);
71 
72 		layout = new GridLayout();
73 		layout.marginHeight = 0;
74 		layout.marginWidth = 0;
75 		//layout.verticalSpacing = 0;
76 		layout.numColumns = 2;
77 		main.setLayout(layout);
78 
79 		scrollable.setContent(main);
80 		scrollable.setExpandVertical(true);
81 		scrollable.setExpandHorizontal(true);
82 
83 		/*
84 		 * Re-adjust scrollbar setting when the window resizes
85 		 */
86 		scrollable.addControlListener(new ControlAdapter() {
87 			public void controlResized(ControlEvent e) {
88 				Rectangle r = scrollable.getClientArea();
89 				scrollable.setMinSize(main.computeSize(r.width, SWT.DEFAULT ));
90 			}
91 		});
92 
93 	    gridData = new GridData(GridData.FILL_BOTH);
94 	    gridData.horizontalSpan = 3;
95 	    Utils.setLayoutData(main, gridData);
96 
97 	    for (int i=0;i<keys.length;i++){
98 
99 	    	if ( keys[i] == null || values[i] == null ){
100 
101 	    		continue;
102 	    	}
103 
104 		    BufferedLabel	msg_label = new BufferedLabel(main, SWT.NULL);
105 		    String msg;
106 		    String key = keys[i];
107 		    if ( key.length() == 0 ){
108 		    	msg = "";
109 		    }else if ( key.startsWith( "!" ) && key.endsWith( "!" )){
110 		    	msg = key.substring(1, key.length()-1 );
111 		    }else{
112 		    	msg = MessageText.getString( key );
113 		    }
114 
115 		    String value = values[i];
116 
117 		    	// hack to allow key values on their own
118 
119 		    if ( value.equals( "<null>" )){
120 
121 		    	msg_label.setText( msg );
122 
123 		    	value = "";
124 		    }else{
125 
126 		    	msg_label.setText( msg.length()==0?"":(msg + ":" ));
127 		    }
128 
129 		    gridData = new GridData();
130 		    gridData.verticalAlignment = GridData.VERTICAL_ALIGN_FILL;
131 		    Utils.setLayoutData(msg_label, gridData);
132 
133 		    BufferedLabel	val_label = new BufferedLabel(main,SWT.WRAP);
134 		    val_label.setText( value );
135 		    gridData = new GridData(GridData.FILL_HORIZONTAL);
136 		    gridData.horizontalIndent = 6;
137 		    Utils.setLayoutData(val_label, gridData);
138 
139 		    field_map.put( key, val_label );
140 	    }
141 
142 			// separator
143 
144 		Label labelSeparator = new Label(shell,SWT.SEPARATOR | SWT.HORIZONTAL);
145 		gridData = new GridData(GridData.FILL_HORIZONTAL);
146 		gridData.horizontalSpan = 3;
147 		Utils.setLayoutData(labelSeparator, gridData);
148 
149 			// buttons
150 
151 		new Label(shell,SWT.NULL);
152 
153 		Button bOk = new Button(shell,SWT.PUSH);
154 	 	Messages.setLanguageText(bOk, "Button.ok");
155 	 	gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_END | GridData.HORIZONTAL_ALIGN_FILL);
156 	 	gridData.grabExcessHorizontalSpace = true;
157 	 	gridData.widthHint = 70;
158 	 	Utils.setLayoutData(bOk, gridData);
159 	 	bOk.addListener(SWT.Selection,new Listener() {
160 	  		public void handleEvent(Event e) {
161 		 		close();
162 	   		}
163 		 });
164 
165 	 	Button bCancel = new Button(shell,SWT.PUSH);
166 	 	Messages.setLanguageText(bCancel, "Button.cancel");
167 	 	gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
168 	 	gridData.grabExcessHorizontalSpace = false;
169 	 	gridData.widthHint = 70;
170 	 	Utils.setLayoutData(bCancel, gridData);
171 	 	bCancel.addListener(SWT.Selection,new Listener() {
172 	 		public void handleEvent(Event e) {
173 		 		close();
174 	   		}
175 	 	});
176 
177 	 	shell.setDefaultButton( bOk );
178 
179 	 	shell.addListener(SWT.Traverse, new Listener() {
180 			public void handleEvent(Event e) {
181 				if ( e.character == SWT.ESC){
182 					close();
183 				}
184 			}
185 		});
186 
187 	 	if (!Utils.linkShellMetricsToConfig(shell, "PropWin")) {
188 	  	 	int	shell_width = 400;
189 
190 	  	 	int	main_height = main.computeSize(shell_width, SWT.DEFAULT).y;
191 
192 	  	 	main_height = Math.max( main_height, 250 );
193 
194 	  	 	main_height = Math.min( main_height, 500 );
195 
196 	  	 	int shell_height = main_height + 50;
197 
198 	  	 	shell.setSize( shell_width, shell_height );
199 	 	}
200 
201 		Utils.centreWindow( shell );
202 
203 		shell.open();
204 	}
205 
206 	public void
updateProperty( final String key, final String value )207 	updateProperty(
208 		final String		key,
209 		final String		value )
210 	{
211 		Utils.execSWTThread(
212 			new Runnable() {
213 
214 				public void run() {
215 
216 					BufferedLabel label = field_map.get( key );
217 
218 					if ( label != null && !label.isDisposed()){
219 
220 						label.setText( value );
221 					}
222 				}
223 			});
224 	}
225 
226 	protected void
close()227 	close()
228 	{
229 		if ( !shell.isDisposed()){
230 
231 			shell.dispose();
232 		}
233 	}
234 }
235