001: /*
002: * Created on May 19, 2004
003: */
004: package net.charabia.jsmoothgen.application.swtgui;
005:
006: import net.charabia.jsmoothgen.application.JSmoothModelBean;
007: import net.charabia.jsmoothgen.application.swtgui.resources.JSmoothResources;
008:
009: import org.eclipse.swt.SWT;
010: import org.eclipse.swt.SWTException;
011: import org.eclipse.swt.events.ModifyEvent;
012: import org.eclipse.swt.events.ModifyListener;
013: import org.eclipse.swt.events.SelectionAdapter;
014: import org.eclipse.swt.events.SelectionEvent;
015: import org.eclipse.swt.graphics.Image;
016: import org.eclipse.swt.graphics.ImageData;
017: import org.eclipse.swt.layout.GridData;
018: import org.eclipse.swt.layout.GridLayout;
019: import org.eclipse.swt.widgets.Button;
020: import org.eclipse.swt.widgets.Composite;
021: import org.eclipse.swt.widgets.Control;
022: import org.eclipse.swt.widgets.DirectoryDialog;
023: import org.eclipse.swt.widgets.FileDialog;
024: import org.eclipse.swt.widgets.Group;
025: import org.eclipse.swt.widgets.Label;
026: import org.eclipse.swt.widgets.Text;
027:
028: public class ExecutablePage extends JSmoothPage {
029: private Text exe;
030: private Text dir;
031: private Label icon;
032:
033: public ExecutablePage(JSmoothApplication js) {
034: super (js);
035: }
036:
037: public Control createPageArea(Composite parent) {
038: Composite top = new Composite(parent, SWT.NONE);
039: top.setLayout(new GridLayout(3, false));
040:
041: // Executable name
042: Label label = new Label(top, SWT.NONE);
043: label.setText("Executable name:");
044: GridData grid = new GridData(GridData.FILL);
045: label.setLayoutData(grid);
046:
047: exe = new Text(top, SWT.BORDER);
048: grid = new GridData(GridData.FILL_HORIZONTAL);
049: grid.widthHint = 250;
050: exe.setLayoutData(grid);
051: exe.addModifyListener(new ModifyListener() {
052: public void modifyText(ModifyEvent e) {
053: setModelExename(exe.getText());
054: }
055: });
056:
057: Button button = new Button(top, SWT.NONE);
058: button.setText("Browse...");
059: button.addSelectionListener(new SelectionAdapter() {
060: public void widgetSelected(SelectionEvent e) {
061: FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
062: dialog.setText("Executable Name");
063: String file = dialog.open();
064: if (file != null)
065: ExecutablePage.this .exe.setText(file);
066: }
067: });
068: grid = new GridData(GridData.FILL);
069: grid.widthHint = 100;
070: button.setLayoutData(grid);
071:
072: // Current directory
073: label = new Label(top, SWT.NONE);
074: label.setText("Current directory:");
075: grid = new GridData(GridData.FILL);
076: label.setLayoutData(grid);
077:
078: dir = new Text(top, SWT.BORDER);
079: grid = new GridData(GridData.FILL_HORIZONTAL);
080: grid.widthHint = 250;
081: dir.setLayoutData(grid);
082: dir.addModifyListener(new ModifyListener() {
083: public void modifyText(ModifyEvent e) {
084: setModelCurrentdir(dir.getText());
085: }
086: });
087:
088: button = new Button(top, SWT.NONE);
089: button.setText("Browse...");
090: button.addSelectionListener(new SelectionAdapter() {
091: public void widgetSelected(SelectionEvent e) {
092: DirectoryDialog dialog = new DirectoryDialog(
093: getShell(), SWT.SAVE);
094: dialog.setText("Current Directory");
095: String dir = dialog.open();
096: if (dir != null)
097: ExecutablePage.this .dir.setText(dir);
098: }
099: });
100: grid = new GridData(GridData.FILL);
101: grid.widthHint = 100;
102: button.setLayoutData(grid);
103:
104: Group group = new Group(top, SWT.NONE);
105: GridLayout layout = new GridLayout();
106: group.setLayout(layout);
107: grid = new GridData(GridData.FILL
108: | GridData.HORIZONTAL_ALIGN_CENTER);
109: grid.horizontalSpan = 3;
110: group.setLayoutData(grid);
111: group.setText("Executable icon");
112:
113: icon = new Label(group, SWT.BORDER | SWT.FLAT);
114: grid = new GridData(GridData.FILL
115: | GridData.HORIZONTAL_ALIGN_CENTER);
116: grid.widthHint = 48;
117: grid.heightHint = 48;
118: icon.setLayoutData(grid);
119:
120: button = new Button(group, SWT.NONE);
121: button.setText("Browse...");
122: button.addSelectionListener(new SelectionAdapter() {
123: public void widgetSelected(SelectionEvent e) {
124: FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
125: dialog.setText("Icon File");
126: String file = dialog.open();
127:
128: // Means "CANCEL"
129: if (file == null)
130: return;
131:
132: setModelIcon(setIcon(file) ? file : null);
133: }
134: });
135: grid = new GridData(GridData.FILL);
136: grid.widthHint = 100;
137: button.setLayoutData(grid);
138:
139: return top;
140: }
141:
142: private void setModelCurrentdir(String dir) {
143: System.out.println("[DEBUG] Setting current directory to: "
144: + dir);
145: JSmoothModelBean jsmodel = getApplication().getModelBean();
146: jsmodel.setCurrentDirectory(dir);
147: }
148:
149: private void setModelExename(String exename) {
150: System.out.println("[DEBUG] Setting exe name to: " + exename);
151: JSmoothModelBean jsmodel = getApplication().getModelBean();
152: jsmodel.setExecutableName(exename);
153: }
154:
155: private boolean setIcon(String file) {
156: Image img = null;
157:
158: img = icon.getImage();
159: if (img != null) {
160: // Clear the label's image
161: icon.setImage(null);
162: img.dispose();
163: }
164:
165: if (file == null || file.equals(""))
166: return true;
167:
168: try {
169: img = new Image(getShell().getDisplay(), file);
170: ImageData data = img.getImageData();
171: if (data.width > 48 && data.height > 48) {
172: // TODO: Output a message to JSmooth Console.
173: System.out
174: .println("[DEBUG] The image size is too big, > 48x48");
175: return false;
176: }
177: } catch (SWTException e) {
178: System.out.println("[ERROR] " + e.getMessage());
179: return false;
180: }
181:
182: icon.setImage(img);
183:
184: return true;
185: }
186:
187: private void setModelIcon(String iconfile) {
188: System.out.println("[DEBUG] Setting icon file to: " + iconfile);
189: JSmoothModelBean jsmodel = getApplication().getModelBean();
190: jsmodel.setIconLocation(iconfile);
191: }
192:
193: protected void configureResources() {
194: setImage(JSmoothResources.IMG_SWITCHER_EXECUTABLE);
195: setToolTip("Windows Executable");
196: }
197:
198: public void load() {
199: JSmoothModelBean jsmodel = getApplication().getModelBean();
200: String exename = jsmodel.getExecutableName();
201: if (exename == null)
202: exename = "";
203: this .exe.setText(exename);
204:
205: String iconfile = jsmodel.getIconLocation();
206: setIcon(iconfile);
207:
208: String dirname = jsmodel.getCurrentDirectory();
209: if (dirname == null)
210: dirname = "";
211: this.dir.setText(dirname);
212: }
213: }
|