Commits

montlikadani authored and md_5 committed 135c1131020
#52: Add GUI button to cancel compilation
No tags

src/main/java/org/spigotmc/gui/panels/general/GeneralPanel.java

Modified
31 31 import java.awt.event.ActionEvent;
32 32 import java.io.IOException;
33 33 import java.util.ArrayList;
34 34 import java.util.List;
35 35 import java.util.concurrent.CompletableFuture;
36 36
37 37 public class GeneralPanel extends JPanel implements Lockable {
38 38
39 39 private final JButton buildButton = new JButton();
40 40 private final JButton copyLogButton = new JButton();
41 + private final JButton cancelButton = new JButton();
41 42 private final JButton debugInfoButton = new JButton();
42 43
43 44 private final JProgressBar progressBar = new JProgressBar();
44 45
45 46 private SettingsPanel settingsPanel;
46 47 private VersionWarningPane versionWarningPane;
47 48 private ConsolePane consolePane;
48 49 private ConsolePanelHeader consolePanelHeader;
49 50
50 51 private final JFrame parent;
81 82 buildButton.setMargin(new Insets(8, 16, 8, 16));
82 83 buildButton.addActionListener(this::buildButtonActionPerformed);
83 84 buildButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
84 85
85 86 copyLogButton.setText("Copy Log");
86 87 copyLogButton.setToolTipText("Copy the log to your clipboard.");
87 88 copyLogButton.setMargin(new Insets(8, 16, 8, 16));
88 89 copyLogButton.addActionListener(this::copyLogButtonActionPerformed);
89 90 copyLogButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
90 91
92 + cancelButton.setText("Cancel");
93 + cancelButton.setToolTipText("Terminates the currently running compilation operation");
94 + cancelButton.setMargin(new Insets(8, 16, 8, 16));
95 + cancelButton.addActionListener(this::cancelButtonActionPerformed);
96 + cancelButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
97 + cancelButton.setEnabled(false);
98 +
91 99 debugInfoButton.setText("Debug Info");
92 100 debugInfoButton.setToolTipText("View the debug panel.");
93 101 debugInfoButton.setMargin(new Insets(8, 16, 8, 16));
94 102 debugInfoButton.addActionListener(this::debugInfoButtonActionPerformed);
95 103 debugInfoButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
96 104
97 105 GroupLayout layout = new GroupLayout(this);
98 106 setLayout(layout);
99 107 layout.setHorizontalGroup(
100 108 layout.createParallelGroup(GroupLayout.Alignment.LEADING)
101 109 .addGroup(layout.createSequentialGroup()
102 110 .addComponent(debugInfoButton)
103 111 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
112 + .addComponent(cancelButton)
113 + .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
104 114 .addComponent(copyLogButton)
105 115 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
106 116 .addComponent(buildButton))
107 117 .addComponent(settingsPanelHeader, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
108 118 .addComponent(consolePane)
109 119 .addComponent(consolePanelHeader, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
110 120 .addComponent(settingsPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
111 121 .addComponent(progressBar, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE,
112 122 GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
113 123 .addComponent(versionWarningPane));
124 134 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
125 135 .addComponent(consolePanelHeader, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
126 136 GroupLayout.PREFERRED_SIZE)
127 137 .addGap(0, 0, 0)
128 138 .addComponent(consolePane, GroupLayout.DEFAULT_SIZE, 418, Short.MAX_VALUE)
129 139 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
130 140 .addComponent(progressBar, GroupLayout.PREFERRED_SIZE, 10, GroupLayout.PREFERRED_SIZE)
131 141 .addGap(8, 8, 8)
132 142 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
133 143 .addComponent(debugInfoButton, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
144 + .addComponent(cancelButton, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
134 145 .addComponent(copyLogButton, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
135 146 .addComponent(buildButton, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE))));
136 147
137 148 setFocusCycleRoot(true);
138 149 }
139 150
140 151 private void buildButtonActionPerformed(ActionEvent event) {
152 + cancelButton.setEnabled(true);
141 153 consolePane.updateConsoleAreaText(new ArrayList<>());
142 154
143 155 progressBar.setIndeterminate(true);
144 156 progressBar.setForeground(Constants.ACCENT_COLOR_DESATURATED);
145 157
146 158 List<String> args = buildData.buildArgs(buildSettings);
147 159
148 160 buildButton.setText("Compiling...");
149 161 SwingUtils.toggleLockComponents(parent, LockReason.BUILD);
150 162
163 175 } catch (IOException e) {
164 176 throw new RuntimeException(e);
165 177 }
166 178
167 179 try {
168 180 return process.waitFor();
169 181 } catch (InterruptedException e) {
170 182 throw new RuntimeException(e);
171 183 }
172 184 }).whenComplete((Integer exitValue, Throwable throwable) -> {
185 + cancelButton.setEnabled(false);
173 186 buildButton.setText("Compile");
174 187 progressBar.setIndeterminate(false);
175 188 progressBar.setForeground(Constants.ACCENT_COLOR);
176 189 progressBar.setValue(100);
177 190
178 191 SwingUtils.toggleLockComponents(parent, LockReason.BUILD);
179 192
180 193 if (consolePanelHeader.copyLogsOnFinish()) {
181 194 SwingUtils.copyToClipboard(new File(Constants.LOG_FILE));
182 195 }
221 234 JFrame debug = new DebugInfoModal(buildData, buildSettings);
222 235 debug.setLocationRelativeTo(null);
223 236 debug.setVisible(true);
224 237 }
225 238
226 239 private void copyLogButtonActionPerformed(ActionEvent event) {
227 240 SwingUtils.copyToClipboard(new File(Constants.LOG_FILE));
228 241 SwingUtils.buttonCooldown((AbstractButton) event.getSource(), 3, "Copied To Clipboard");
229 242 }
230 243
244 + private void cancelButtonActionPerformed(ActionEvent event) {
245 + cancelButton.setEnabled(false);
246 +
247 + if (buildProcess == null || buildProcess.getProcess() == null) {
248 + return;
249 + }
250 +
251 + buildProcess.getProcess().destroy();
252 + System.out.println("Compilation terminated by the user");
253 + }
254 +
231 255 public SettingsPanel getSettingsPanel() {
232 256 return settingsPanel;
233 257 }
234 258
235 259 public VersionWarningPane getVersionWarningPane() {
236 260 return versionWarningPane;
237 261 }
238 262
239 263 public ConsolePane getConsolePane() {
240 264 return consolePane;

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut