Skip to content

Commit

Permalink
Bump to v0.5.1 improved remote connectionmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaOonk committed Jun 23, 2021
1 parent b50d109 commit 49c0e31
Show file tree
Hide file tree
Showing 17 changed files with 581 additions and 55 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.lucaoonk.Virt_Commander</groupId>
<artifactId>Virt_Commander</artifactId>
<packaging>jar</packaging>
<version>0.4.1</version>
<version>0.5.1</version>
<name>Virt_Commander</name>
<url>http://maven.apache.org</url>

Expand Down
134 changes: 130 additions & 4 deletions src/main/java/com/lucaoonk/Virt_Commander/Backend/Objects/Context.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package com.lucaoonk.Virt_Commander.Backend.Objects;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Map;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lucaoonk.Virt_Commander.Backend.Processors.Local.VMDOMProcessorThread;
import com.lucaoonk.Virt_Commander.Backend.Processors.Local.VMListProcessor;
import com.lucaoonk.Virt_Commander.Backend.Processors.Remote.RemoteVMListProcessor;
Expand All @@ -14,6 +22,8 @@
import com.lucaoonk.Virt_Commander.ui.ScrollableVMList;
import com.lucaoonk.Virt_Commander.ui.VMDetailsPanel;

import org.json.simple.JSONObject;

public class Context {

public static String latestVersion;
Expand All @@ -25,7 +35,7 @@ public class Context {
public MainContent mainContent;
public JFrame mainJFrame;
public String defaultSaveLocation;
private static final String versionString = "0.5";
private static final String versionString = "0.5.1";
public Boolean checkForUpdates;
private String applicationDefaultSaveLocation;
public Integer windowHeight;
Expand All @@ -35,21 +45,25 @@ public class Context {
public long autoRefreshRate;
public boolean local;
public String remoteAddress;
public boolean remoteOrLocal;
private String currentSelectedUUID;

public ArrayList<RemoteConnection> remoteConnections;
private final static String connectionsFileLocation = System.getProperty("user.home") + "/Library/Application Support/Virt_Commander/connections.json";


public Context(){
initDefaults();
this.autoRefresh = true;
this.autoRefreshRate =10;
}

readConnectionsFile();
}

private void initDefaults(){
this.checkForUpdates = true;
this.remoteOrLocal = false;
this.local = true;
this.remoteAddress = "";
this.remoteConnections = new ArrayList<RemoteConnection>();

this.defaultSaveLocation=System.getProperty("user.home")+"/vms/";
this.applicationDefaultSaveLocation=System.getProperty("user.home")+"/vms/";
Expand All @@ -69,6 +83,30 @@ public ArrayList<VM> getVMList(){
return this.vmList;
}

public RemoteConnectionComboItem[] getRemoteConnectionComboItems(){
readConnectionsFile();

if(this.remoteConnections == null || this.remoteConnections.size() == 0){

RemoteConnection r = new RemoteConnection();
r.name = "No connections";
RemoteConnectionComboItem[] array = new RemoteConnectionComboItem[1];
array[0] = new RemoteConnectionComboItem(r);

return array;

}else{
RemoteConnectionComboItem[] array = new RemoteConnectionComboItem[this.remoteConnections.size()];
int i = 0;
for (RemoteConnection connection : remoteConnections) {
array[i] = new RemoteConnectionComboItem(connection);
i++;
}

return array;
}
}

public VM getCurrentSelectedVM(){

for (VM vm : vmList) {
Expand Down Expand Up @@ -141,4 +179,92 @@ public String getDefaultSaveLocation() {
}

}


public void writeConnections(){

JSONObject jsonObject = new JSONObject();
Gson g = new Gson();


jsonObject.put("connections", g.toJson(remoteConnections));


File location = new File(connectionsFileLocation);
location.getParentFile().mkdirs();

FileWriter file;
try {
if(Context.connectionsFileExists()){


file = new FileWriter(connectionsFileLocation, false);

file.write(jsonObject.toJSONString());
file.close();
}else{

file = new FileWriter(connectionsFileLocation);


file.write(jsonObject.toJSONString());
file.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static Boolean connectionsFileExists(){

File f = new File(connectionsFileLocation);
if(f.exists() && !f.isDirectory()) {
return true;
}else{
return false;
}


}

public void readConnectionsFile(){

if(Context.connectionsFileExists()){

try {
// create Gson instance
Gson gson = new Gson();

// create a reader
Reader reader = Files.newBufferedReader(Paths.get(connectionsFileLocation));

// convert JSON file to map
Map<?, ?> map = gson.fromJson(reader, Map.class);

// print map entries
for (Map.Entry<?, ?> entry : map.entrySet()) {
if(entry.getKey().equals("connections")){
Gson g = new Gson();
remoteConnections = g.fromJson((String) entry.getValue(),new TypeToken<ArrayList<RemoteConnection>>() {
}.getType());
}
}

// close reader
reader.close();

} catch (Exception ex) {
ex.printStackTrace();
}

}else{
System.out.println("Connections-File does not exists. Using defaults");

}


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public class Device {
public String target;



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.lucaoonk.Virt_Commander.Backend.Objects;

public class RemoteConnection {

public String name;
public String address;
public Boolean favorite;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.lucaoonk.Virt_Commander.Backend.Objects;

public class RemoteConnectionComboItem {

private String key;
private RemoteConnection value;

public RemoteConnectionComboItem(){

}

public RemoteConnectionComboItem(RemoteConnection connection)
{
this.key = connection.name + " | "+connection.address;
this.value = connection;
}

@Override
public String toString()
{
return key;
}

public String getKey()
{
return key;
}

public RemoteConnection getValue()
{
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,26 @@ public String vmDetailsTable(){


String disksString = "";
String devicesString = "";

int amountOfDisks = 0;
int amountOfDevices = 0;
for (Device dev : this.getDevices()) {
if(dev.getClass().getName().equals("com.lucaoonk.virsh_gui.Backend.Objects.Disk")){
if(dev.getClass().getName().equals("com.lucaoonk.Virt_Commander.Backend.Objects.Disk")){
amountOfDisks+=1;
Disk disk = (Disk) dev;

disksString+= disk.device + ":"+"<br>"+"&nbsp;Location: "+ disk.source + "<br>&nbsp;Type: "+ disk.driver + "<br><br>";

}else{
amountOfDevices+=1;

devicesString+= dev.device + "<br><br>";
}

}
vmDetails+= "<tr><td>Attached Disks ("+amountOfDisks+") :</td><td>"+disksString+"</td></tr>";
vmDetails+= "<tr><td>Attached Devices ("+amountOfDevices+") :</td><td>"+devicesString+"</td></tr>";

String forwardedPorts = "";
for (String port : this.getForwardedPorts()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.lucaoonk.Virt_Commander.Backend.Processors.Remote;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;

import com.lucaoonk.Virt_Commander.Backend.Objects.Context;

public class RemoteVMListActionListener implements ActionListener{

private Context context;
private JDialog dialog;

public RemoteVMListActionListener(Context context, JDialog dialog) {
this.context = context;
this.dialog = dialog;
}

@Override
public void actionPerformed(ActionEvent e) {
this.dialog.dispose();
context.local = true;
context.refresh();

}



}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.lucaoonk.Virt_Commander.Backend.Processors.Remote;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
Expand All @@ -28,12 +31,12 @@ public RemoteVMListProcessor(Context context){
}

public void runCommand() throws IOException{
this.vmList = getVMdomainList(context.remoteAddress);
this.vmList = getVMdomainList(context.remoteAddress, context);

context.updateVMList(vmList);
}

public static ArrayList<VM> getVMdomainList(String remoteAddress) throws IOException {
public static ArrayList<VM> getVMdomainList(String remoteAddress, Context context) throws IOException {

try {
HttpResponse<String> response = Unirest.get(remoteAddress+"/list")
Expand All @@ -55,10 +58,16 @@ public static ArrayList<VM> getVMdomainList(String remoteAddress) throws IOExcep

JDialog dialog = new JDialog();
JPanel panel = new JPanel();
JButton switchToLocalButton = new JButton("Switch to local");
// switchToLocalButton.addActionListener(new SwitchToLocalListener());
dialog.setTitle("Error connecting to remote server!");
JLabel label = new JLabel("<html><b>Make sure u enterd the correct address</b></html>");
JLabel label = new JLabel("<html><b>Make sure u entered the correct address</b></html>");
switchToLocalButton.addActionListener(new RemoteVMListActionListener(context, dialog));
panel.setBorder(new EmptyBorder(10,10,10,10));
panel.add(label);
panel.add(switchToLocalButton);

// panel.add(switchToLocalButton);
dialog.add(panel);
dialog.setSize(300, 150);
dialog.setLocationRelativeTo(null);
Expand All @@ -73,3 +82,5 @@ public static ArrayList<VM> getVMdomainList(String remoteAddress) throws IOExcep
}

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.lucaoonk.Virt_Commander.Backend.Processors.Remote;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SwitchToLocalListener implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}

}
Loading

0 comments on commit 49c0e31

Please sign in to comment.