gdx-studio

#libgdx#java#desktop

git clone https://git.pyrossh.dev/gdx-studio

An IDE for creating Games using libgdx and Java supported on all platforms Android, iOS, Desktop


src/gdxstudio/panel/ConsolePanel.java
a9b2a64 1
package gdxstudio.panel;
a9b2a64 2
a9b2a64 3
import gdxstudio.Content;
a9b2a64 4
import gdxstudio.GdxStudio;
a9b2a64 5
import gdxstudio.StatusBar;
a9b2a64 6
import gdxstudio.Style;
a9b2a64 7
a62d533 8
import java.awt.Color;
a62d533 9
import java.awt.Dimension;
a62d533 10
import java.awt.Insets;
a62d533 11
import java.awt.event.ActionEvent;
a62d533 12
import java.awt.event.ActionListener;
a62d533 13
import java.io.File;
a62d533 14
import java.io.IOException;
a62d533 15
import java.io.OutputStream;
a62d533 16
import java.io.PrintStream;
a62d533 17
import java.io.PrintWriter;
a62d533 18
a62d533 19
import javax.swing.JPanel;
a62d533 20
import javax.swing.JScrollPane;
a62d533 21
import javax.swing.JTextArea;
a62d533 22
import javax.swing.SwingUtilities;
a62d533 23
a62d533 24
a62d533 25
a62d533 26
a62d533 27
a62d533 28
7c84877 29
a9b2a64 30
a9b2a64 31
a62d533 32
import web.laf.lite.layout.VerticalFlowLayout;
a62d533 33
import web.laf.lite.utils.UIUtils;
a62d533 34
a62d533 35
import org.eclipse.jdt.core.compiler.CompilationProgress;
a62d533 36
import org.eclipse.jdt.core.compiler.batch.BatchCompiler;
a62d533 37
a62d533 38
public class ConsolePanel extends JPanel implements ActionListener {
a62d533 39
	private static final long serialVersionUID = 1L;
a62d533 40
	
a62d533 41
	private static final JTextArea consoleArea = new JTextArea("");
a62d533 42
	private static final JScrollPane consoleAreaPane = new JScrollPane(consoleArea);
a62d533 43
	
a62d533 44
	public static String classPath;
a62d533 45
	public static int errorCount = 0;
a62d533 46
	public static int warningCount = 0;
a62d533 47
a62d533 48
	public ConsolePanel(){
a62d533 49
		super(new VerticalFlowLayout());
a62d533 50
		UIUtils.setFontSize(consoleArea, 14);
a62d533 51
		UIUtils.setFontName(consoleArea, Content.editor.getFont().getFontName());
a62d533 52
		UIUtils.setMargin(this, new Insets(0,0,0,0));
a62d533 53
	    UIUtils.setUndecorated(this, true);
a9b2a64 54
        consoleAreaPane.setPreferredSize(new Dimension(500, 200));
7c84877 55
		add(new Style.TitleButton("Console", this));
a62d533 56
		setVisible(false);
a62d533 57
        add(consoleAreaPane);
a62d533 58
        if(Content.projectExists())
a62d533 59
        	updateCompiler();
a62d533 60
	}
a62d533 61
	
a62d533 62
	public static void updateCompiler(){
a62d533 63
		String filename = ConsolePanel.class.getProtectionDomain().getCodeSource().getLocation().getFile();
a9b2a64 64
		GdxStudio.log(filename);
a62d533 65
		if(filename.endsWith(".jar"))
a62d533 66
			classPath = filename;
a62d533 67
		else
a62d533 68
			classPath = filename+File.pathSeparator+
a62d533 69
			new File(filename).getParentFile().getPath()+"/libs/gdx.jar"+File.pathSeparator;
a62d533 70
	}
a62d533 71
a62d533 72
	public static void redirectSystemStreams(){
a62d533 73
		final OutputStream out = new OutputStream() {
a62d533 74
		      @Override
a62d533 75
		      public void write(int b) throws IOException {
a62d533 76
		          ConsolePanel.updateConsoleArea(String.valueOf((char) b));
a62d533 77
		      }
a62d533 78
		
a62d533 79
		      @Override
a62d533 80
		      public void write(byte[] b, int off, int len) throws IOException {
a62d533 81
		    	  ConsolePanel.updateConsoleArea(new String(b, off, len));
a62d533 82
		      }
a62d533 83
		
a62d533 84
		      @Override
a62d533 85
		      public void write(byte[] b) throws IOException {
a62d533 86
		          write(b, 0, b.length);
a62d533 87
		      }
a62d533 88
		 };
a62d533 89
		 final OutputStream err = new OutputStream() {
a62d533 90
			      @Override
a62d533 91
			      public void write(int b) throws IOException {
a62d533 92
			    	  consoleArea.setText("");
a62d533 93
			          ConsolePanel.updateConsoleArea(String.valueOf((char) b));
a62d533 94
			      }
a62d533 95
			
a62d533 96
			      @Override
a62d533 97
			      public void write(byte[] b, int off, int len) throws IOException {
a62d533 98
			    	  consoleArea.setText("");
a62d533 99
			    	  ConsolePanel.updateConsoleArea(new String(b, off, len));
a62d533 100
			      }
a62d533 101
			
a62d533 102
			      @Override
a62d533 103
			      public void write(byte[] b) throws IOException {
a62d533 104
			        write(b, 0, b.length);
a62d533 105
			      }
a62d533 106
		};
a62d533 107
		System.setOut(new PrintStream(out, true));
a62d533 108
		System.setErr(new PrintStream(err, true));
a62d533 109
	}
a62d533 110
a62d533 111
	public static void updateConsoleArea(final String text) {
a62d533 112
	    SwingUtilities.invokeLater(new Runnable() {
a62d533 113
	      public void run() {
a62d533 114
	    	  consoleArea.append(text);
a62d533 115
	    	  consoleArea.setCaretPosition(consoleArea.getText().length());
a62d533 116
	      }
a62d533 117
	    });
a62d533 118
	  }
a62d533 119
a62d533 120
	@Override
a62d533 121
	public void actionPerformed(ActionEvent e) {
a62d533 122
		setVisible(false);
a62d533 123
	}
a62d533 124
	
a62d533 125
	static String errString = "";
a62d533 126
	static Color warningColor = new Color(255, 240, 0);
a62d533 127
	final static CompilationProgress prog = null;
a62d533 128
	final static PrintWriter compilerWriter = new PrintWriter(new OutputStream() {
a62d533 129
		@Override
a62d533 130
		public void write(int b) throws IOException {
a62d533 131
		}
a62d533 132
a62d533 133
		@Override
a62d533 134
		public void write(byte[] b, int off, int len) throws IOException {
a62d533 135
			errString = new String(b, off, len);
a62d533 136
			consoleArea.setText("");
a9b2a64 137
			GdxStudio.log(errString);
a62d533 138
			for(String line: errString.split(System.lineSeparator())){
a9b2a64 139
				if(line.contains("warning")){
a62d533 140
					String[] warningLine = line.split(":");
a62d533 141
					warningCount++;
a62d533 142
					Content.editor.addWarning(Integer.parseInt(warningLine[warningLine.length-3]),
a62d533 143
							warningLine[warningLine.length-1]);
a62d533 144
				}
a9b2a64 145
				if(line.contains("error")){
a62d533 146
					String[] errorLine = line.split(":");
a62d533 147
					errorCount++;
a62d533 148
					Content.editor.addError(Integer.parseInt(errorLine[errorLine.length-3]),
a62d533 149
							errorLine[errorLine.length-1]);
a62d533 150
				}
a62d533 151
			}
a62d533 152
			if(warningCount == 1){
a62d533 153
				StatusBar.warning.setForeground(warningColor);
a62d533 154
				StatusBar.warning.setText("1 Warning");
a62d533 155
			}
a62d533 156
			if(warningCount > 1){
a62d533 157
				StatusBar.warning.setForeground(warningColor);
a62d533 158
				StatusBar.warning.setText(warningCount+" Warnings");
a62d533 159
			}
a62d533 160
			if(errorCount == 1){
a62d533 161
				StatusBar.error.setForeground(Color.red);
a62d533 162
				StatusBar.error.setText("1 Error");
a62d533 163
			}
a62d533 164
			if(errorCount>1){
a62d533 165
				StatusBar.error.setForeground(Color.red);
a62d533 166
				StatusBar.error.setText(errorCount+" Errors");
a62d533 167
			}
a62d533 168
		}
a62d533 169
a62d533 170
		@Override
a62d533 171
		public void write(byte[] b) throws IOException {
a62d533 172
			write(b, 0, b.length);
a62d533 173
		}
a62d533 174
	});
a62d533 175
a9b2a64 176
	public static void compile(final String file) {
a9b2a64 177
		clear();
a62d533 178
		Content.editor.clearIcons();
a62d533 179
		StatusBar.error.setForeground(Color.black);
a62d533 180
		StatusBar.error.setText("Errors");
a62d533 181
		StatusBar.warning.setForeground(Color.black);
a62d533 182
		StatusBar.warning.setText("Warnings");
a62d533 183
		errorCount = 0;
a62d533 184
		warningCount = 0;
a9b2a64 185
		consoleArea.append("Compiling... "+file+".java"+System.lineSeparator());
a9b2a64 186
		final String buildArgs = Content.getProject()+"source/"+ file+".java"
a9b2a64 187
				+" -cp "+classPath
a9b2a64 188
				+" -d "+Content.getProject()+"bin"
a9b2a64 189
				+" -1.7"
a9b2a64 190
				+" -Xemacs";
a9b2a64 191
		Thread t = new Thread(new Runnable(){
a9b2a64 192
			@Override
a9b2a64 193
			public void run() {
a9b2a64 194
				BatchCompiler.compile(buildArgs, compilerWriter, compilerWriter, prog);
a9b2a64 195
				consoleArea.append("Finished Compiling... "+file+".java"+System.lineSeparator());
a9b2a64 196
				System.gc();
a9b2a64 197
			}
a9b2a64 198
		});
a9b2a64 199
		t.start();
a9b2a64 200
	}
a9b2a64 201
	
a9b2a64 202
	public static void build() {
a9b2a64 203
		clear();
a9b2a64 204
		consoleArea.append("Building.. "+Content.getProject()+System.lineSeparator());
a9b2a64 205
		final String buildArgs = Content.getProject()+"source/"
a62d533 206
				+" -cp "+classPath
a62d533 207
				+" -d "+Content.getProject()+"bin"
a62d533 208
				+" -1.7"
a62d533 209
				+" -Xemacs";
a62d533 210
		Thread t = new Thread(new Runnable(){
a62d533 211
			@Override
a62d533 212
			public void run() {
a62d533 213
				BatchCompiler.compile(buildArgs, compilerWriter, compilerWriter, prog);
a9b2a64 214
				System.gc();
a62d533 215
			}
a62d533 216
		});
a62d533 217
		t.start();
a62d533 218
	}
a62d533 219
	
a62d533 220
	public static void clear(){
a62d533 221
		consoleArea.setText("");
a62d533 222
	}
a62d533 223
}