Skip to content

Commit 8695a77

Browse files
committed
GUI is done. GL getting CLI to work
1 parent b12af29 commit 8695a77

36 files changed

Lines changed: 1001 additions & 805 deletions

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
sourceCompatibility = JavaVersion.VERSION_1_8
88
targetCompatibility = JavaVersion.VERSION_1_8
99

10-
mainClassName = 'org.mcphackers.mcp.cli.MainCLI'
10+
mainClassName = 'org.mcphackers.mcp.main.MainGUI'
1111

1212
repositories {
1313
mavenCentral()
@@ -40,7 +40,7 @@ dependencies {
4040

4141
application {
4242
// Redirect all execution into the testing folder
43-
mainClass = 'org.mcphackers.mcp.gui.MainGUI'
43+
mainClass = 'org.mcphackers.mcp.main.MainGUI'
4444
executableDir = 'test'
4545
}
4646

logs/mcp.log

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Operating system: Windows 7
2+
RetroMCP v0.4
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
/*
2+
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of Oracle or the names of its
16+
* contributors may be used to endorse or promote products derived
17+
* from this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20+
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
package layout;
33+
34+
import javax.swing.*;
35+
import javax.swing.SpringLayout;
36+
import java.awt.*;
37+
38+
/**
39+
* A 1.4 file that provides utility methods for
40+
* creating form- or grid-style layouts with SpringLayout.
41+
* These utilities are used by several programs, such as
42+
* SpringBox and SpringCompactGrid.
43+
*/
44+
public class SpringUtilities {
45+
/**
46+
* A debugging utility that prints to stdout the component's
47+
* minimum, preferred, and maximum sizes.
48+
*/
49+
public static void printSizes(Component c) {
50+
System.out.println("minimumSize = " + c.getMinimumSize());
51+
System.out.println("preferredSize = " + c.getPreferredSize());
52+
System.out.println("maximumSize = " + c.getMaximumSize());
53+
}
54+
55+
/**
56+
* Aligns the first <code>rows</code> * <code>cols</code>
57+
* components of <code>parent</code> in
58+
* a grid. Each component is as big as the maximum
59+
* preferred width and height of the components.
60+
* The parent is made just big enough to fit them all.
61+
*
62+
* @param rows number of rows
63+
* @param cols number of columns
64+
* @param initialX x location to start the grid at
65+
* @param initialY y location to start the grid at
66+
* @param xPad x padding between cells
67+
* @param yPad y padding between cells
68+
*/
69+
public static void makeGrid(Container parent,
70+
int rows, int cols,
71+
int initialX, int initialY,
72+
int xPad, int yPad) {
73+
SpringLayout layout;
74+
try {
75+
layout = (SpringLayout)parent.getLayout();
76+
} catch (ClassCastException exc) {
77+
System.err.println("The first argument to makeGrid must use SpringLayout.");
78+
return;
79+
}
80+
81+
Spring xPadSpring = Spring.constant(xPad);
82+
Spring yPadSpring = Spring.constant(yPad);
83+
Spring initialXSpring = Spring.constant(initialX);
84+
Spring initialYSpring = Spring.constant(initialY);
85+
int max = rows * cols;
86+
87+
//Calculate Springs that are the max of the width/height so that all
88+
//cells have the same size.
89+
Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)).
90+
getWidth();
91+
Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)).
92+
getHeight();
93+
for (int i = 1; i < max; i++) {
94+
SpringLayout.Constraints cons = layout.getConstraints(
95+
parent.getComponent(i));
96+
97+
maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
98+
maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
99+
}
100+
101+
//Apply the new width/height Spring. This forces all the
102+
//components to have the same size.
103+
for (int i = 0; i < max; i++) {
104+
SpringLayout.Constraints cons = layout.getConstraints(
105+
parent.getComponent(i));
106+
107+
cons.setWidth(maxWidthSpring);
108+
cons.setHeight(maxHeightSpring);
109+
}
110+
111+
//Then adjust the x/y constraints of all the cells so that they
112+
//are aligned in a grid.
113+
SpringLayout.Constraints lastCons = null;
114+
SpringLayout.Constraints lastRowCons = null;
115+
for (int i = 0; i < max; i++) {
116+
SpringLayout.Constraints cons = layout.getConstraints(
117+
parent.getComponent(i));
118+
if (i % cols == 0) { //start of new row
119+
lastRowCons = lastCons;
120+
cons.setX(initialXSpring);
121+
} else { //x position depends on previous component
122+
cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST),
123+
xPadSpring));
124+
}
125+
126+
if (i / cols == 0) { //first row
127+
cons.setY(initialYSpring);
128+
} else { //y position depends on previous row
129+
cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH),
130+
yPadSpring));
131+
}
132+
lastCons = cons;
133+
}
134+
135+
//Set the parent's size.
136+
SpringLayout.Constraints pCons = layout.getConstraints(parent);
137+
pCons.setConstraint(SpringLayout.SOUTH,
138+
Spring.sum(
139+
Spring.constant(yPad),
140+
lastCons.getConstraint(SpringLayout.SOUTH)));
141+
pCons.setConstraint(SpringLayout.EAST,
142+
Spring.sum(
143+
Spring.constant(xPad),
144+
lastCons.getConstraint(SpringLayout.EAST)));
145+
}
146+
147+
/* Used by makeCompactGrid. */
148+
private static SpringLayout.Constraints getConstraintsForCell(
149+
int row, int col,
150+
Container parent,
151+
int cols) {
152+
SpringLayout layout = (SpringLayout) parent.getLayout();
153+
Component c = parent.getComponent(row * cols + col);
154+
return layout.getConstraints(c);
155+
}
156+
157+
/**
158+
* Aligns the first <code>rows</code> * <code>cols</code>
159+
* components of <code>parent</code> in
160+
* a grid. Each component in a column is as wide as the maximum
161+
* preferred width of the components in that column;
162+
* height is similarly determined for each row.
163+
* The parent is made just big enough to fit them all.
164+
*
165+
* @param rows number of rows
166+
* @param cols number of columns
167+
* @param initialX x location to start the grid at
168+
* @param initialY y location to start the grid at
169+
* @param xPad x padding between cells
170+
* @param yPad y padding between cells
171+
*/
172+
public static void makeCompactGrid(Container parent,
173+
int rows, int cols,
174+
int initialX, int initialY,
175+
int xPad, int yPad) {
176+
SpringLayout layout;
177+
try {
178+
layout = (SpringLayout)parent.getLayout();
179+
} catch (ClassCastException exc) {
180+
System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
181+
return;
182+
}
183+
184+
//Align all cells in each column and make them the same width.
185+
Spring x = Spring.constant(initialX);
186+
for (int c = 0; c < cols; c++) {
187+
Spring width = Spring.constant(0);
188+
for (int r = 0; r < rows; r++) {
189+
width = Spring.max(width,
190+
getConstraintsForCell(r, c, parent, cols).
191+
getWidth());
192+
}
193+
for (int r = 0; r < rows; r++) {
194+
SpringLayout.Constraints constraints =
195+
getConstraintsForCell(r, c, parent, cols);
196+
constraints.setX(x);
197+
constraints.setWidth(width);
198+
}
199+
x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
200+
}
201+
202+
//Align all cells in each row and make them the same height.
203+
Spring y = Spring.constant(initialY);
204+
for (int r = 0; r < rows; r++) {
205+
Spring height = Spring.constant(0);
206+
for (int c = 0; c < cols; c++) {
207+
height = Spring.max(height,
208+
getConstraintsForCell(r, c, parent, cols).
209+
getHeight());
210+
}
211+
for (int c = 0; c < cols; c++) {
212+
SpringLayout.Constraints constraints =
213+
getConstraintsForCell(r, c, parent, cols);
214+
constraints.setY(y);
215+
constraints.setHeight(height);
216+
}
217+
y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
218+
}
219+
220+
//Set the parent's size.
221+
SpringLayout.Constraints pCons = layout.getConstraints(parent);
222+
pCons.setConstraint(SpringLayout.SOUTH, y);
223+
pCons.setConstraint(SpringLayout.EAST, x);
224+
}
225+
}

src/main/java/org/mcphackers/mcp/MCP.java

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
import java.io.IOException;
44
import java.nio.file.Files;
55
import java.nio.file.Paths;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.concurrent.ExecutorService;
9+
import java.util.concurrent.Executors;
10+
import java.util.concurrent.atomic.AtomicInteger;
11+
12+
import org.mcphackers.mcp.tasks.Task;
13+
import org.mcphackers.mcp.tasks.Task.Side;
14+
import org.mcphackers.mcp.tools.VersionsParser;
615

716
public interface MCP {
817

@@ -21,21 +30,124 @@ default void attemptToDeleteUpdateJar() {
2130
}
2231
}
2332

33+
default void performTask(TaskMode mode, Side side) {
34+
performTask(mode, side, true, true);
35+
}
36+
37+
default void performTask(TaskMode mode, Side side, boolean enableProgressBars, boolean enableCompletionMessage) {
38+
List<Task> tasks = mode.getTasks(this);
39+
if(tasks.size() == 0) {
40+
System.err.println("Performing 0 tasks");
41+
return;
42+
}
43+
44+
boolean hasServer = true;
45+
try {
46+
hasServer = VersionsParser.hasServer(getCurrentVersion());
47+
} catch (Exception e) {
48+
e.printStackTrace();
49+
}
50+
List<Task> performedTasks = new ArrayList<>();
51+
for (Task task : tasks) {
52+
if (side == Side.ANY) {
53+
// TODO also check if client/server exists locally
54+
if (task.side == Side.SERVER && hasServer || task.side != Side.SERVER) {
55+
performedTasks.add(task);
56+
}
57+
} else if (task.side == side) {
58+
performedTasks.add(task);
59+
}
60+
}
61+
setProgressBars(performedTasks);
62+
ExecutorService pool = Executors.newFixedThreadPool(2);
63+
setActive(false);
64+
65+
AtomicInteger result1 = new AtomicInteger(Task.INFO);
66+
67+
for(int i = 0; i < performedTasks.size(); i++) {
68+
Task task = performedTasks.get(i);
69+
final int barIndex = i;
70+
if(enableProgressBars) {
71+
task.setProgressBarIndex(barIndex);
72+
}
73+
pool.execute(() -> {
74+
String name = mode.name;
75+
if(task.side == Side.CLIENT || task.side == Side.SERVER) {
76+
name = task.side.name;
77+
}
78+
if(enableProgressBars) {
79+
setProgressBarName(barIndex, name);
80+
setProgressBarActive(barIndex, true);
81+
}
82+
try {
83+
task.doTask();
84+
} catch (Exception e) {
85+
result1.set(Task.ERROR);
86+
e.printStackTrace();
87+
}
88+
if(enableProgressBars) {
89+
setProgress(barIndex, "Finished!", 100);
90+
}
91+
});
92+
}
93+
pool.shutdown();
94+
while (!pool.isTerminated()) {}
95+
96+
byte result = result1.byteValue();
97+
98+
List<String> msgs = new ArrayList<>();
99+
for(Task task : performedTasks) {
100+
msgs.addAll(task.getMessageList());
101+
byte retresult = task.getResult();
102+
if(retresult > result) {
103+
result = retresult;
104+
}
105+
}
106+
if(msgs.size() > 0) log("");
107+
for(String msg : msgs) {
108+
log(msg);
109+
}
110+
if(enableCompletionMessage) {
111+
String[] msgs2 = {"Finished successfully!", "Finished with warnings!", "Finished with errors!"};
112+
showMessage(mode.name, msgs2[result], result);
113+
}
114+
115+
for(int i = 0; i < performedTasks.size(); i++) {
116+
setProgressBarActive(i, false);
117+
118+
setProgress(i, "Idle", 0);
119+
}
120+
setActive(true);
121+
clearProgressBars();
122+
}
123+
124+
void setProgressBars(List<Task> tasks);
125+
126+
void clearProgressBars();
127+
24128
void log(String msg);
25129

26130
Options getOptions();
27131

132+
String getCurrentVersion();
133+
134+
void setCurrentVersion(String version);
135+
28136
void setProgressBarActive(int barIndex, boolean active);
29137

138+
void setProgressBarName(int side, String name);
139+
30140
void setProgress(int barIndex, String progressMessage);
31141

32142
void setProgress(int barIndex, int progress);
143+
144+
void setActive(boolean active);
33145

34146
boolean yesNoInput(String title, String msg);
35147

36148
String inputString(String title, String msg);
37149

38-
void showPopup(String title, String msg, int type);
150+
void showMessage(String title, String msg, int type);
39151

40152
default void setProgress(int barIndex, String progressMessage, int progress) {
41153
setProgress(barIndex, progress);

src/main/java/org/mcphackers/mcp/MCPLogger.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.fusesource.jansi.Ansi;
1010
import org.mcphackers.mcp.tools.FileUtil;
1111

12+
//FIXME unused class
1213
public class MCPLogger {
1314

1415
private BufferedWriter writer;

0 commit comments

Comments
 (0)