1 /*
2 	Launch4j (http://launch4j.sourceforge.net/)
3 	Cross-platform Java application wrapper for creating Windows native executables.
4 
5 	Copyright (c) 2004, 2015 Grzegorz Kowal
6 	All rights reserved.
7 
8 	Redistribution and use in source and binary forms, with or without modification,
9 	are permitted provided that the following conditions are met:
10 
11 	1. Redistributions of source code must retain the above copyright notice,
12 	   this list of conditions and the following disclaimer.
13 
14 	2. Redistributions in binary form must reproduce the above copyright notice,
15 	   this list of conditions and the following disclaimer in the documentation
16 	   and/or other materials provided with the distribution.
17 
18 	3. Neither the name of the copyright holder nor the names of its contributors
19 	   may be used to endorse or promote products derived from this software without
20 	   specific prior written permission.
21 
22 	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 	THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26 	FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 	AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 	OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 	OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 /*
35  * Created on May 1, 2006
36  */
37 package net.sf.launch4j.formimpl;
38 
39 import java.awt.event.ActionEvent;
40 import java.awt.event.ActionListener;
41 
42 import javax.swing.DefaultComboBoxModel;
43 import javax.swing.JFileChooser;
44 import javax.swing.JTextField;
45 
46 import net.sf.launch4j.binding.Bindings;
47 import net.sf.launch4j.binding.Validator;
48 import net.sf.launch4j.form.JreForm;
49 import net.sf.launch4j.config.Jre;
50 
51 /**
52  * @author Copyright (C) 2006 Grzegorz Kowal
53  */
54 public class JreFormImpl extends JreForm {
55 
JreFormImpl(Bindings bindings, JFileChooser fc)56 	public JreFormImpl(Bindings bindings, JFileChooser fc) {
57 		_jdkPreferenceCombo.setModel(new DefaultComboBoxModel(new String[] {
58 				Messages.getString("jdkPreference.jre.only"),
59 				Messages.getString("jdkPreference.prefer.jre"),
60 				Messages.getString("jdkPreference.prefer.jdk"),
61 				Messages.getString("jdkPreference.jdk.only")}));
62 
63 		_runtimeBitsCombo.setModel(new DefaultComboBoxModel(new String[] {
64 				Messages.getString("runtimeBits.64"),
65 				Messages.getString("runtimeBits.64And32"),
66 				Messages.getString("runtimeBits.32And64"),
67 				Messages.getString("runtimeBits.32")}));
68 
69 		bindings.add("jre.path", _jrePathField)
70 				.add("jre.bundledJre64Bit", _bundledJre64BitCheck)
71 				.add("jre.bundledJreAsFallback", _bundledJreAsFallbackCheck)
72 				.add("jre.minVersion", _jreMinField)
73 				.add("jre.maxVersion", _jreMaxField)
74 				.add("jre.jdkPreferenceIndex", _jdkPreferenceCombo,	Jre.DEFAULT_JDK_PREFERENCE_INDEX)
75 				.add("jre.runtimeBitsIndex", _runtimeBitsCombo, Jre.DEFAULT_JDK_PREFERENCE_INDEX)
76 				.add("jre.initialHeapSize", _initialHeapSizeField)
77 				.add("jre.initialHeapPercent", _initialHeapPercentField)
78 				.add("jre.maxHeapSize", _maxHeapSizeField)
79 				.add("jre.maxHeapPercent", _maxHeapPercentField)
80 				.add("jre.options", _jvmOptionsTextArea);
81 
82 		_varCombo.setModel(new DefaultComboBoxModel(new String[] {
83 				"EXEDIR", "EXEFILE", "PWD", "OLDPWD", "JREHOMEDIR",
84 				"HKEY_CLASSES_ROOT", "HKEY_CURRENT_USER", "HKEY_LOCAL_MACHINE",
85 				"HKEY_USERS", "HKEY_CURRENT_CONFIG" }));
86 
87 		_varCombo.addActionListener(new VarComboActionListener());
88 		_varCombo.setSelectedIndex(0);
89 
90 		_propertyButton.addActionListener(new PropertyActionListener());
91 		_optionButton.addActionListener(new OptionActionListener());
92 
93 		_envPropertyButton.addActionListener(new EnvPropertyActionListener(_envVarField));
94 		_envOptionButton.addActionListener(new EnvOptionActionListener(_envVarField));
95 	}
96 
97 	private class VarComboActionListener implements ActionListener {
actionPerformed(ActionEvent e)98 		public void actionPerformed(ActionEvent e) {
99 			_optionButton.setEnabled(((String) _varCombo.getSelectedItem())
100 					.startsWith("HKEY_"));
101 		}
102 	}
103 
104 	private class PropertyActionListener implements ActionListener {
actionPerformed(ActionEvent e)105 		public void actionPerformed(ActionEvent e) {
106 			final int pos = _jvmOptionsTextArea.getCaretPosition();
107 			final String var = (String) _varCombo.getSelectedItem();
108 			if (var.startsWith("HKEY_")) {
109 				_jvmOptionsTextArea.insert("-Dreg.key=\"%"
110 						+ var + "\\...%\"\n", pos);
111 			} else {
112 				_jvmOptionsTextArea.insert("-Dlaunch4j." + var.toLowerCase()
113 						+ "=\"%" + var + "%\"\n", pos);
114 			}
115 		}
116 	}
117 
118 	private class OptionActionListener implements ActionListener {
actionPerformed(ActionEvent e)119 		public void actionPerformed(ActionEvent e) {
120 			final int pos = _jvmOptionsTextArea.getCaretPosition();
121 			final String var = (String) _varCombo.getSelectedItem();
122 			if (var.startsWith("HKEY_")) {
123 				_jvmOptionsTextArea.insert("%" + var + "\\...%\n", pos);
124 			} else {
125 				_jvmOptionsTextArea.insert("%" + var + "%\n", pos);
126 			}
127 		}
128 	}
129 
130 	private abstract class EnvActionListener extends AbstractAcceptListener {
EnvActionListener(JTextField f, boolean listen)131 		public EnvActionListener(JTextField f, boolean listen) {
132 			super(f, listen);
133 		}
134 
actionPerformed(ActionEvent e)135 		public void actionPerformed(ActionEvent e) {
136 			final int pos = _jvmOptionsTextArea.getCaretPosition();
137 			final String var = getText()
138 					.replaceAll("\"", "")
139 					.replaceAll("%", "");
140 			if (Validator.isEmpty(var)) {
141 				signalViolation(Messages.getString("specifyVar"));
142 				return;
143 			}
144 			add(var, pos);
145 			clear();
146 		}
147 
add(String var, int pos)148 		protected abstract void add(String var, int pos);
149 	}
150 
151 	private class EnvPropertyActionListener extends EnvActionListener {
EnvPropertyActionListener(JTextField f)152 		public EnvPropertyActionListener(JTextField f) {
153 			super(f, true);
154 		}
155 
add(String var, int pos)156 		protected void add(String var, int pos) {
157 			final String prop = var
158 					.replaceAll(" ", ".")
159 					.replaceAll("_", ".")
160 					.toLowerCase();
161 			_jvmOptionsTextArea.insert("-Denv." + prop + "=\"%" + var
162 					+ "%\"\n", pos);
163 		}
164 	}
165 
166 	private class EnvOptionActionListener extends EnvActionListener {
EnvOptionActionListener(JTextField f)167 		public EnvOptionActionListener(JTextField f) {
168 			super(f, false);
169 		}
170 
add(String var, int pos)171 		protected void add(String var, int pos) {
172 			_jvmOptionsTextArea.insert("%" + var + "%\n", pos);
173 		}
174 	}
175 }
176