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/Update.java
a9b2a64 1
package gdxstudio;
a62d533 2
import java.io.BufferedReader;
a62d533 3
import java.io.File;
a62d533 4
import java.io.FileOutputStream;
a62d533 5
import java.io.IOException;
a62d533 6
import java.io.InputStream;
a62d533 7
import java.io.InputStreamReader;
a62d533 8
a62d533 9
import javax.swing.JOptionPane;
a62d533 10
import javax.swing.SwingWorker;
a62d533 11
a62d533 12
import java.net.HttpURLConnection;
a62d533 13
import java.net.MalformedURLException;
a62d533 14
import java.net.URL;
a62d533 15
import java.nio.file.Files;
a62d533 16
import java.nio.file.StandardCopyOption;
a62d533 17
import java.awt.Dimension;
a62d533 18
import java.awt.GridBagConstraints;
a62d533 19
import java.awt.GridBagLayout;
a62d533 20
import java.awt.Insets;
a62d533 21
import java.awt.event.ActionEvent;
a62d533 22
import java.awt.event.ActionListener;
a62d533 23
import java.beans.PropertyChangeEvent;
a62d533 24
import java.beans.PropertyChangeListener;
a62d533 25
a62d533 26
import javax.swing.JButton;
a62d533 27
import javax.swing.JFrame;
a62d533 28
import javax.swing.JLabel;
a62d533 29
import javax.swing.JProgressBar;
a62d533 30
import javax.swing.JTextField;
a62d533 31
a62d533 32
/**
a62d533 33
 * A Swing application that downloads file from an HTTP server.
a62d533 34
 * @author www.codejava.net
a62d533 35
 *
a62d533 36
 */
a62d533 37
public class Update extends JFrame implements PropertyChangeListener, ActionListener {
a62d533 38
	private static final long serialVersionUID = 1L;
a62d533 39
	private JLabel labelFileName = new JLabel("File name: ");
a62d533 40
	private JTextField fieldFileName = new JTextField(20);
a62d533 41
	
a62d533 42
	private JLabel labelFileSize = new JLabel("File size (bytes): ");
a62d533 43
	private JTextField fieldFileSize = new JTextField(20);
a62d533 44
	
a62d533 45
	private JLabel labelProgress = new JLabel("Progress:");
a62d533 46
	private JProgressBar progressBar = new JProgressBar(0, 100);
a62d533 47
	
a62d533 48
	private JButton updateButton = new JButton("Update");
a62d533 49
	
a62d533 50
	public static String[] libs = {
a62d533 51
		"adnroid", "antlr", "autocomplete", "bluecove-glp", "bluecove", "dx", "ecj", 
a62d533 52
		"gdx-backend-android", "gdx-backend-lwjgl-natives", "gdx-backend-lwjgl", "gdx-natives", "gdx",
a62d533 53
		"proguard", "rsta", "socketio", "weblaflite"
a62d533 54
	};
a62d533 55
	
a62d533 56
	String workDir = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
a62d533 57
	String saveDir = workDir+"temp";
a62d533 58
	
a62d533 59
	public Update() {
a62d533 60
		super("Swing File Download from HTTP server");
a62d533 61
		System.setProperty("jsse.enableSNIExtension", "false");
a62d533 62
		if(checkNewVersion());
a62d533 63
			//startDownload("gdx.jar");
a62d533 64
		else
a62d533 65
			return;
a62d533 66
		setLayout(new GridBagLayout());
a62d533 67
		GridBagConstraints constraints = new GridBagConstraints();
a62d533 68
		constraints.anchor = GridBagConstraints.WEST;
a62d533 69
		constraints.insets = new Insets(5, 5, 5, 5);
a62d533 70
a62d533 71
		fieldFileName.setEditable(false);
a62d533 72
		fieldFileSize.setEditable(false);
a62d533 73
		
a62d533 74
		progressBar.setPreferredSize(new Dimension(200, 30));
a62d533 75
		progressBar.setStringPainted(true);
a62d533 76
		
a62d533 77
		constraints.gridx = 0;
a62d533 78
		constraints.gridy = 1;
a62d533 79
		constraints.weightx = 0.0;
a62d533 80
		constraints.gridwidth = 2;
a62d533 81
		constraints.fill = GridBagConstraints.NONE;
a62d533 82
		constraints.gridy = 2;
a62d533 83
		constraints.anchor = GridBagConstraints.CENTER;
a62d533 84
		add(updateButton, constraints);
a62d533 85
		updateButton.addActionListener(this);
a62d533 86
		
a62d533 87
		constraints.gridx = 0;
a62d533 88
		constraints.gridy = 3;
a62d533 89
		constraints.gridwidth = 1;
a62d533 90
		constraints.anchor = GridBagConstraints.WEST;
a62d533 91
		add(labelFileName, constraints);
a62d533 92
		
a62d533 93
		constraints.gridx = 1;
a62d533 94
		add(fieldFileName, constraints);
a62d533 95
		
a62d533 96
		constraints.gridy = 4;
a62d533 97
		constraints.gridx = 0;
a62d533 98
		add(labelFileSize, constraints);
a62d533 99
		
a62d533 100
		constraints.gridx = 1;
a62d533 101
		add(fieldFileSize, constraints);
a62d533 102
		
a62d533 103
		constraints.gridx = 0;
a62d533 104
		constraints.gridy = 5;
a62d533 105
		constraints.gridwidth = 1;
a62d533 106
		constraints.anchor = GridBagConstraints.WEST;
a62d533 107
		add(labelProgress, constraints);
a62d533 108
a62d533 109
		constraints.gridx = 1;
a62d533 110
		constraints.weightx = 1.0;
a62d533 111
		constraints.fill = GridBagConstraints.HORIZONTAL;
a62d533 112
		add(progressBar, constraints);
a62d533 113
a62d533 114
		pack();
a62d533 115
		setLocationRelativeTo(null);	// center on screen
a62d533 116
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a62d533 117
	}
a62d533 118
	@Override
a62d533 119
	public void actionPerformed(ActionEvent e){
a62d533 120
		new File(saveDir).mkdir();
a62d533 121
		String file = "gdx.jar";
a62d533 122
		//for(String file: libs){
a62d533 123
		try {
a62d533 124
			progressBar.setValue(0);
a62d533 125
			int serverFileLength = getContentLength("https://github.com/pyros2097/Scene3d/raw/master/libs/"+file);
a62d533 126
			long localFileLength = new File(workDir+"libs/"+file).length();
a9b2a64 127
			GdxStudio.log("serverFileLength:"+serverFileLength+" localFileLength:"+localFileLength);
a62d533 128
			if(serverFileLength == localFileLength)
a62d533 129
				return;//continue;
a62d533 130
			AsyncDownloader as = new AsyncDownloader("https://github.com/pyros2097/Scene3d/raw/master/libs/", file);
a62d533 131
			as.addPropertyChangeListener(this);
a62d533 132
			
a62d533 133
		} catch (Exception ex) {
a62d533 134
			JOptionPane.showMessageDialog(this, "Error executing upload task: " + ex.getMessage(), "Error",
a62d533 135
					JOptionPane.ERROR_MESSAGE);
a62d533 136
		}		
a62d533 137
		//}
a62d533 138
		new File(saveDir).deleteOnExit();
a62d533 139
	}
a62d533 140
	
a62d533 141
	public String getNewVersion(){
a62d533 142
		String line = "";
a62d533 143
		HttpURLConnection httpConn = openConnection("https://github.com/pyros2097/GdxStudio/raw/master/README.md");
a62d533 144
		if(httpConn == null)
a9b2a64 145
			return GdxStudio.version;
a62d533 146
		if(isOK(httpConn)) {
a62d533 147
			try {
a62d533 148
				BufferedReader br = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
a62d533 149
				line = br.readLine();
a62d533 150
				br.close();
a62d533 151
				httpConn.getInputStream().close();
a62d533 152
				httpConn.disconnect();
a62d533 153
			} catch (IOException e) {
a62d533 154
				e.printStackTrace();
a62d533 155
			}
a62d533 156
		}
a62d533 157
		return line.substring(10);
a62d533 158
	}
a62d533 159
	
a62d533 160
	public HttpURLConnection openConnection(String url){
a62d533 161
		try {
a62d533 162
			return (HttpURLConnection) new URL(url).openConnection();
a62d533 163
		} catch (MalformedURLException e) {
a62d533 164
			e.printStackTrace();
a62d533 165
		} catch (IOException e) {
a62d533 166
			e.printStackTrace();
a62d533 167
		}
a62d533 168
		return null;
a62d533 169
	}
a62d533 170
	
a62d533 171
	public boolean isOK(HttpURLConnection httpConn){
a62d533 172
		try {
a62d533 173
			return (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK);
a62d533 174
		} catch (IOException e) {
a62d533 175
			e.printStackTrace();
a62d533 176
		}
a62d533 177
		return false;
a62d533 178
	}
a62d533 179
	
a62d533 180
	public int getContentLength(String fileURL){
a62d533 181
		HttpURLConnection httpConn = openConnection(fileURL);
a62d533 182
		int length = getContentLength(httpConn);
a62d533 183
		httpConn.disconnect();
a62d533 184
		return length;
a62d533 185
	}
a62d533 186
	
a62d533 187
	public int getContentLength(HttpURLConnection httpConn){
a62d533 188
		int length = 0;
a62d533 189
		if(httpConn == null)
a62d533 190
			return length;
a62d533 191
		if(isOK(httpConn)) 
a62d533 192
			length = httpConn.getContentLength();
a62d533 193
		return length;
a62d533 194
	}
a62d533 195
	
a62d533 196
	private boolean checkNewVersion(){
a9b2a64 197
		String[] vals1 = GdxStudio.version.split("\\.");
a62d533 198
		String[] vals2 = getNewVersion().split("\\.");
a62d533 199
		int i=0;
a62d533 200
		while(i<vals1.length && i<vals2.length && vals1[i].equals(vals2[i])) {
a62d533 201
		  i++;
a62d533 202
		}
a62d533 203
		if (i<vals1.length && i<vals2.length) {
a62d533 204
		    int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
a62d533 205
		    if(Integer.signum(diff) != 1)
a62d533 206
		    	return true;
a62d533 207
		}
a62d533 208
		return false;
a62d533 209
	}
a62d533 210
a62d533 211
	void setFileInfo(String name, int size) {
a62d533 212
		fieldFileName.setText(name);
a62d533 213
		fieldFileSize.setText(String.valueOf(size));
a62d533 214
	}
a62d533 215
	
a62d533 216
	/**
a62d533 217
	 * Update the progress bar's state whenever the progress of download changes.
a62d533 218
	 */	
a62d533 219
	@Override
a62d533 220
	public void propertyChange(PropertyChangeEvent evt) {
a62d533 221
		if (evt.getPropertyName().equals("progress")) {
a62d533 222
			int progress = (Integer) evt.getNewValue();
a62d533 223
			progressBar.setValue(progress);
a62d533 224
		}
a62d533 225
	}
a62d533 226
	
a62d533 227
	/**
a62d533 228
	 * Execute file download in a background thread and update the progress. 
a62d533 229
	 * @author www.codejava.net
a62d533 230
	 *
a62d533 231
	 */
a62d533 232
	public class AsyncDownloader extends SwingWorker<Void, Void> {
a62d533 233
		private static final int BUFFER_SIZE = 4096;	
a62d533 234
		private String downloadURL;
a62d533 235
		private File saveFile;
a62d533 236
		
a62d533 237
		public AsyncDownloader(String downloadURL, String fileName) {
a62d533 238
			this.downloadURL = downloadURL+fileName;
a62d533 239
			saveFile = new File(saveDir+ File.separator + fileName);
a62d533 240
			execute();
a62d533 241
		}
a62d533 242
		
a62d533 243
		byte[] buffer = new byte[BUFFER_SIZE];
a62d533 244
		int bytesRead = -1;
a62d533 245
		long totalBytesRead = 0;
a62d533 246
		int percentCompleted = 0;
a62d533 247
		long fileSize;
a62d533 248
		HttpURLConnection conn;
a62d533 249
		/**
a62d533 250
		 * Executed in background thread
a62d533 251
		 */	
a62d533 252
		@Override
a62d533 253
		protected Void doInBackground() throws Exception {
a62d533 254
			try {
a62d533 255
				conn = openConnection(downloadURL);
a62d533 256
				if(conn == null)
a62d533 257
					return null;
a62d533 258
				fileSize = getContentLength(conn);
a62d533 259
				setFileInfo(saveFile.getName(), getContentLength(conn));
a62d533 260
				InputStream inputStream = conn.getInputStream();
a62d533 261
				FileOutputStream outputStream = new FileOutputStream(saveFile.getPath());
a62d533 262
			
a62d533 263
				while ((bytesRead = inputStream.read(buffer)) != -1) {
a62d533 264
					outputStream.write(buffer, 0, bytesRead);
a62d533 265
					totalBytesRead += bytesRead;
a62d533 266
					percentCompleted = (int) (totalBytesRead * 100 / fileSize);
a62d533 267
	
a62d533 268
					setProgress(percentCompleted);			
a62d533 269
				}
a62d533 270
	
a62d533 271
				outputStream.close();
a62d533 272
				inputStream.close();
a62d533 273
				conn.disconnect();
a62d533 274
			} catch (IOException ex) {
a62d533 275
				JOptionPane.showMessageDialog(null, "Error downloading file: " + ex.getMessage(),
a62d533 276
						"Error", JOptionPane.ERROR_MESSAGE);			
a62d533 277
				ex.printStackTrace();
a62d533 278
				setProgress(0);
a62d533 279
				cancel(true);			
a62d533 280
			}
a62d533 281
			return null;
a62d533 282
		}
a62d533 283
	
a62d533 284
		/**
a62d533 285
		 * Executed in Swing's event dispatching thread
a62d533 286
		 */
a62d533 287
		@Override
a62d533 288
		protected void done() {
a62d533 289
			if (!isCancelled()) {
a62d533 290
				try {
a62d533 291
					Files.copy(saveFile.toPath(), // replace existing files with downloaded files
a62d533 292
							new File(workDir+"libs/"+saveFile.getName()).toPath(), 
a62d533 293
							StandardCopyOption.REPLACE_EXISTING);
a62d533 294
				} catch (IOException e1) {
a62d533 295
					e1.printStackTrace();
a62d533 296
				}
a62d533 297
				JOptionPane.showMessageDialog(null, "File has been downloaded successfully!", "Message",
a62d533 298
						JOptionPane.INFORMATION_MESSAGE);
a62d533 299
			}
a62d533 300
		}	
a62d533 301
	}
a62d533 302
}