1/*******************************************************************************
2 * Copyright 2011 See AUTHORS file.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *   http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************/
16
17package com.badlogic.gdx.setup;
18
19import static java.awt.GridBagConstraints.BOTH;
20import static java.awt.GridBagConstraints.CENTER;
21import static java.awt.GridBagConstraints.HORIZONTAL;
22import static java.awt.GridBagConstraints.NONE;
23import static java.awt.GridBagConstraints.NORTH;
24import static java.awt.GridBagConstraints.SOUTH;
25import static java.awt.GridBagConstraints.SOUTHEAST;
26import static java.awt.GridBagConstraints.WEST;
27
28import java.awt.Color;
29import java.awt.Cursor;
30import java.awt.Desktop;
31import java.awt.GridBagConstraints;
32import java.awt.GridBagLayout;
33import java.awt.Insets;
34import java.awt.event.ActionEvent;
35import java.awt.event.ActionListener;
36import java.awt.event.MouseAdapter;
37import java.awt.event.MouseEvent;
38import java.io.IOException;
39import java.net.URI;
40import java.net.URISyntaxException;
41import java.util.ArrayList;
42import java.util.List;
43
44import javax.swing.BorderFactory;
45import javax.swing.JDialog;
46import javax.swing.JLabel;
47import javax.swing.JOptionPane;
48import javax.swing.JPanel;
49import javax.swing.JSeparator;
50import javax.swing.JTextField;
51import javax.swing.border.Border;
52import javax.swing.border.CompoundBorder;
53import javax.swing.border.EmptyBorder;
54
55import com.badlogic.gdx.setup.GdxSetupUI.SetupButton;
56import com.badlogic.gdx.setup.GdxSetupUI.SetupCheckBox;
57
58public class SettingsDialog extends JDialog {
59
60	private JPanel contentPane;
61	private SetupButton buttonOK;
62	private SetupButton buttonCancel;
63	private JLabel linkText;
64	private JPanel content;
65	private JPanel bottomPanel;
66	private JPanel buttonPanel;
67
68	private JTextField mavenTextField;
69	private SetupCheckBox ideaBox;
70	private SetupCheckBox eclipseBox;
71	SetupCheckBox offlineBox;
72	private String mavenSnapshot;
73	private boolean ideaSnapshot;
74	private boolean eclipseSnapshot;
75	private boolean offlineSnapshot;
76
77	public SettingsDialog () {
78		contentPane = new JPanel(new GridBagLayout());
79		setContentPane(contentPane);
80		setModal(true);
81		getRootPane().setDefaultButton(buttonOK);
82
83		uiLayout();
84		uiStyle();
85
86		buttonOK.addActionListener(new ActionListener() {
87			public void actionPerformed (ActionEvent e) {
88				if (offlineBox.isSelected()) {
89					int value = JOptionPane.showConfirmDialog(null, "You have selected offline mode. This requires you to have your dependencies already in your maven/gradle cache.\n\nThe setup will fail if you do not have the correct dependenices already.\n\nDo you want to continue?", "Warning!", JOptionPane.YES_NO_OPTION);
90					if (value == 0) {
91						onOK();
92					}
93				} else {
94					onOK();
95				}
96			}
97		});
98		buttonCancel.addActionListener(new ActionListener() {
99			@Override
100			public void actionPerformed (ActionEvent e) {
101				onCancel();
102			}
103		});
104
105		linkText.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
106		linkText.addMouseListener(new MouseAdapter() {
107			public void mouseClicked (MouseEvent e) {
108				if (e.getClickCount() > 0) {
109					if (Desktop.isDesktopSupported()) {
110						Desktop desktop = Desktop.getDesktop();
111						try {
112							URI uri = new URI(
113								"https://github.com/libgdx/libgdx/wiki/Improving-workflow-with-Gradle#how-to-remove-gradle-ide-integration-from-your-project");
114							desktop.browse(uri);
115						} catch (IOException ex) {
116							ex.printStackTrace();
117						} catch (URISyntaxException ex) {
118							ex.printStackTrace();
119						}
120					}
121				}
122			}
123		});
124
125		setTitle("Advanced Settings");
126		setSize(600, 300);
127		setLocationRelativeTo(null);
128	}
129
130	private void uiLayout () {
131		content = new JPanel(new GridBagLayout());
132		content.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
133
134		bottomPanel = new JPanel(new GridBagLayout());
135
136		buttonPanel = new JPanel(new GridBagLayout());
137		buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
138
139		buttonOK = new SetupButton("Save");
140		buttonCancel = new SetupButton("Cancel");
141		buttonPanel.add(buttonOK, new GridBagConstraints(0, 0, 1, 1, 0, 0, CENTER, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
142		buttonPanel.add(buttonCancel, new GridBagConstraints(1, 0, 1, 1, 0, 0, CENTER, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
143
144		contentPane.add(content, new GridBagConstraints(0, 0, 1, 1, 1, 1, NORTH, BOTH, new Insets(0, 0, 0, 0), 0, 0));
145
146		JLabel settings = new JLabel("Settings");
147		JLabel description = new JLabel("Description");
148		settings.setForeground(new Color(255, 255, 255));
149		description.setForeground(new Color(255, 255, 255));
150
151		settings.setHorizontalAlignment(JLabel.CENTER);
152		description.setHorizontalAlignment(JLabel.CENTER);
153
154		content.add(settings, new GridBagConstraints(0, 0, 1, 1, 1, 1, NORTH, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
155		content.add(description, new GridBagConstraints(3, 0, 1, 1, 1, 1, NORTH, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
156
157		JLabel mavenLabel = new JLabel("Maven Mirror Url");
158		JLabel mavenDesc = new JLabel("Replaces Maven Central with this repository");
159		mavenTextField = new JTextField(15);
160		mavenTextField.setMinimumSize(mavenTextField.getPreferredSize());
161		mavenLabel.setForeground(new Color(170, 170, 170));
162		mavenDesc.setForeground(new Color(170, 170, 170));
163		JLabel ideaLabel = new JLabel("IDEA");
164		JLabel ideaDesc = new JLabel("Generates Intellij IDEA project files");
165		ideaBox = new SetupCheckBox();
166		ideaLabel.setForeground(new Color(170, 170, 170));
167		ideaDesc.setForeground(new Color(170, 170, 170));
168		ideaBox.setBackground(new Color(36, 36, 36));
169		JLabel eclipseLabel = new JLabel("Eclipse");
170		JLabel eclipseDesc = new JLabel("Generates Eclipse project files");
171		eclipseBox = new SetupCheckBox();
172		eclipseLabel.setForeground(new Color(170, 170, 170));
173		eclipseDesc.setForeground(new Color(170, 170, 170));
174		eclipseBox.setBackground(new Color(36, 36, 36));
175		JLabel offlineLabel = new JLabel("Offline Mode");
176		JLabel offlineDesc = new JLabel("Don't force download dependencies");
177		offlineBox = new SetupCheckBox();
178		offlineLabel.setForeground(new Color(170, 170, 170));
179		offlineDesc.setForeground(new Color(170, 170, 170));
180		offlineBox.setBackground(new Color(36, 36, 36));
181
182		JSeparator separator = new JSeparator();
183		separator.setForeground(new Color(85, 85, 85));
184		separator.setBackground(new Color(85, 85, 85));
185
186		content.add(separator, new GridBagConstraints(0, 1, 4, 1, 1, 1, NORTH, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
187
188		content.add(mavenLabel, new GridBagConstraints(0, 2, 1, 1, 1, 1, NORTH, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
189		content.add(mavenTextField, new GridBagConstraints(1, 2, 2, 1, 1, 1, NORTH, HORIZONTAL, new Insets(0, 15, 0, 0), 0, 0));
190		content.add(mavenDesc, new GridBagConstraints(3, 2, 1, 1, 1, 1, NORTH, HORIZONTAL, new Insets(0, 15, 0, 0), 0, 0));
191
192		content.add(ideaLabel, new GridBagConstraints(0, 3, 1, 1, 1, 1, NORTH, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
193		content.add(ideaBox, new GridBagConstraints(1, 3, 2, 1, 1, 1, NORTH, HORIZONTAL, new Insets(0, 15, 0, 0), 0, 0));
194		content.add(ideaDesc, new GridBagConstraints(3, 3, 2, 1, 1, 1, NORTH, HORIZONTAL, new Insets(0, 15, 0, 0), 0, 0));
195
196		content.add(eclipseLabel, new GridBagConstraints(0, 4, 1, 1, 1, 1, NORTH, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
197		content.add(eclipseBox, new GridBagConstraints(1, 4, 2, 1, 1, 1, NORTH, HORIZONTAL, new Insets(0, 15, 0, 0), 0, 0));
198		content.add(eclipseDesc, new GridBagConstraints(3, 4, 1, 1, 1, 1, NORTH, HORIZONTAL, new Insets(0, 15, 0, 0), 0, 0));
199
200		content.add(offlineLabel, new GridBagConstraints(0, 5, 1, 1, 1, 1, NORTH, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
201		content.add(offlineBox, new GridBagConstraints(1, 5, 2, 1, 1, 1, NORTH, HORIZONTAL, new Insets(0, 15, 0, 0), 0, 0));
202		content.add(offlineDesc, new GridBagConstraints(3, 5, 1, 1, 1, 1, NORTH, HORIZONTAL, new Insets(0, 15, 0, 0), 0, 0));
203
204
205		String text = "<p style=\"font-size:10\">Click for more info on using Gradle without IDE integration</p>";
206		linkText = new JLabel("<html>" + text + "</html>");
207
208		bottomPanel.add(linkText, new GridBagConstraints(0, 0, 1, 1, 1, 1, WEST, NONE, new Insets(0, 10, 0, 0), 0, 0));
209		bottomPanel.add(buttonPanel, new GridBagConstraints(3, 0, 1, 1, 1, 1, SOUTHEAST, NONE, new Insets(0, 0, 0, 0), 0, 0));
210
211		contentPane.add(bottomPanel, new GridBagConstraints(0, 1, 4, 1, 1, 1, SOUTH, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
212
213	}
214
215	private void uiStyle () {
216		content.setBackground(new Color(36, 36, 36));
217		content.setForeground(new Color(255, 255, 255));
218		bottomPanel.setBackground(new Color(36, 36, 36));
219		bottomPanel.setForeground(new Color(255, 255, 255));
220		buttonPanel.setBackground(new Color(36, 36, 36));
221		buttonPanel.setForeground(new Color(255, 255, 255));
222		linkText.setForeground(new Color(20, 150, 20));
223
224		contentPane.setBackground(new Color(36, 36, 36));
225		Border line = BorderFactory.createLineBorder(new Color(80, 80, 80));
226		Border empty = new EmptyBorder(4, 4, 4, 4);
227		CompoundBorder border = new CompoundBorder(line, empty);
228		mavenTextField.setBorder(border);
229		mavenTextField.setCaretColor(new Color(255, 255, 255));
230		mavenTextField.setBackground(new Color(46, 46, 46));
231		mavenTextField.setForeground(new Color(255, 255, 255));
232	}
233
234	public void showDialog () {
235		takeSnapshot();
236		setVisible(true);
237	}
238
239	public List<String> getGradleArgs () {
240		List<String> list = new ArrayList<String>();
241		list.add("--no-daemon");
242		if (offlineBox.isSelected()) {
243			list.add("--offline");
244		}
245		if (eclipseBox.isSelected()) {
246			list.add("eclipse");
247			list.add("afterEclipseImport");
248		}
249		if (ideaBox.isSelected()) {
250			list.add("idea");
251		}
252		return list;
253	}
254
255	void onOK () {
256		if (mavenTextField.getText().isEmpty()) {
257			DependencyBank.mavenCentral = "mavenCentral()";
258		} else {
259			DependencyBank.mavenCentral = "maven { url \"" + mavenTextField.getText() + "\" }";
260		}
261		setVisible(false);
262	}
263
264	void onCancel () {
265		setVisible(false);
266		restore();
267	}
268
269	private void takeSnapshot () {
270		mavenSnapshot = mavenTextField.getText();
271		ideaSnapshot = ideaBox.isSelected();
272		eclipseSnapshot = eclipseBox.isSelected();
273		offlineSnapshot = offlineBox.isSelected();
274	}
275
276	private void restore () {
277		mavenTextField.setText(mavenSnapshot);
278		ideaBox.setSelected(ideaSnapshot);
279		eclipseBox.setSelected(eclipseSnapshot);
280		offlineBox.setSelected(offlineSnapshot);
281	}
282
283}
284