Skip to content

Commit

Permalink
Buttons and Lists of Pastes
Browse files Browse the repository at this point in the history
+ more bug fixes
Rate limit · GitHub

Access has been restricted

You have triggered a rate limit.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

CmdEngineer committed Jan 7, 2018
1 parent 808b933 commit 48338ef
Showing 3 changed files with 260 additions and 35 deletions.
178 changes: 153 additions & 25 deletions src/main/java/com/cmdengineer/pastefile/App.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
package com.cmdengineer.pastefile;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ResponseHandler;

import com.cmdengineer.pastefile.utils.HTTPRequestAPI;
import com.cmdengineer.pastefile.utils.Paste;
import com.cmdengineer.pastefile.utils.Server;
import com.mrcrayfish.device.api.app.Application;
import com.mrcrayfish.device.api.app.Component;
import com.mrcrayfish.device.api.app.Dialog;
import com.mrcrayfish.device.api.app.Icons;
import com.mrcrayfish.device.api.app.Layout;
import com.mrcrayfish.device.api.app.Layout.Background;
import com.mrcrayfish.device.api.app.component.Button;
import com.mrcrayfish.device.api.app.component.ComboBox.List;
import com.mrcrayfish.device.api.app.component.ComboBox;
import com.mrcrayfish.device.api.app.component.ItemList;
import com.mrcrayfish.device.api.app.listener.ClickListener;
import com.mrcrayfish.device.api.app.listener.ItemClickListener;
import com.mrcrayfish.device.api.app.renderer.ListItemRenderer;
import com.mrcrayfish.device.api.app.component.Spinner;
import com.mrcrayfish.device.api.app.component.Text;
import com.mrcrayfish.device.api.app.component.TextField;
import com.mrcrayfish.device.api.io.File;
import com.mrcrayfish.device.core.Laptop;
import com.mrcrayfish.device.programs.ApplicationExample;
import com.mrcrayfish.device.programs.ApplicationTest;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
@@ -26,34 +36,60 @@

public class App extends Application {

int width = 180, height = 100;
int width = 200, height = 144;
// txt -> Text
// txf -> TextField
// btn -> Button
// spn -> Spinner
// itm -> ItemList.

// DATA
Server server;
List<Paste> pastes;

// APP
Layout main;
Boolean hasSelected;

Spinner spnLoad;
/*
* MAIN HUB:
*
*/

TextField txfUser;
int txfUserWidth;


Button btnRefresh;
int btnRefreshX, btnRefreshWidth;

Button btnImport;
int btnImportX, btnImportWidth;

Button btnExport;
int btnExportX, btnExportWidth;

Button btnRemove;
int btnRemoveX, btnRemoveWidth;

Button btnEdit;
int btnEditX, btnEditWidth;

Button btnSettings;
int btnSettingsX, btnSettingsWidth;

/*
* PASTE LIST:
*
*/
ItemList<Paste> itmPastes;

@Override
public void init() {
server = new Server("http://localhost:3001/");

pastes = server.getPastes(Minecraft.getMinecraft().player.getUniqueID().toString());
hasSelected = false;

main = new Layout(width, height);

main.setBackground(new Background() {
@@ -65,52 +101,144 @@ public void render(Gui gui, Minecraft mc, int x, int y, int width, int height, i
});

// txfUser
txfUserWidth = (int)(Minecraft.getMinecraft().player.getName().length() * 8.5) % (width/2);
txfUser = new TextField(0, 0, txfUserWidth);
txfUserWidth = (int)(Minecraft.getMinecraft().player.getName().length() * 9) % (width/2);
txfUser = new TextField(0, 0, txfUserWidth);
txfUser.setEditable(false);
txfUser.setIcon(Icons.USER);
txfUser.setText(Minecraft.getMinecraft().player.getName());
main.addComponent(txfUser);


// btnRefresh
btnRefreshWidth = 16;
btnRefreshX = txfUserWidth + 1;
btnRefresh = new Button(btnRefreshX, 0, Icons.RELOAD);
btnRefresh.setToolTip("Reload:", "Reloads the pastes list.");
btnRefresh.setClickListener(new ClickListener() {
@Override public void onClick(int mouseX, int mouseY, int mouseButton) { btnRefreshPress(mouseX, mouseY, mouseButton); }
});
main.addComponent(btnRefresh);

// btnImport
btnImportWidth = 16;
btnImportX = txfUserWidth + 1;
btnImportX = btnRefreshX + btnRefreshWidth + 1;
btnImport = new Button(btnImportX, 0, Icons.IMPORT);
btnImport.setToolTip("Import:", "Download a file using a file code.");
btnImport.setClickListener(new ClickListener() {
@Override public void onClick(int mouseX, int mouseY, int mouseButton) { btnImportPress(mouseX, mouseY, mouseButton); }
});
main.addComponent(btnImport);

// btnExport
btnExportWidth = 16;
btnExportX = txfUserWidth + btnImportWidth + 2;
btnExportX = btnImportX + btnImportWidth + 1;
btnExport = new Button(btnExportX, 0, Icons.EXPORT);
btnExport.setToolTip("Export:", "Upload a file.");
btnExport.setClickListener(new ClickListener() {
@Override public void onClick(int mouseX, int mouseY, int mouseButton) { btnExportPress(mouseX, mouseY, mouseButton); }
});
main.addComponent(btnExport);

// btnSettings
btnSettingsWidth = 16;
btnSettingsX = width - btnSettingsWidth - 1;
btnSettings = new Button(btnSettingsX, 0, Icons.WRENCH);
btnSettings.setToolTip("Settings:", "Open the settings menu.");
main.addComponent(btnSettings);

// btnRemove
btnRemoveWidth = 16;
btnRemoveX = btnSettingsX - btnRemoveWidth - 1;
btnRemove = new Button(btnRemoveX, 0, Icons.TRASH);
btnRemove.setEnabled(false);
btnRemove.setToolTip("Remove:", "Remove the selected paste.");
btnRemove.setClickListener(new ClickListener() {
@Override public void onClick(int mouseX, int mouseY, int mouseButton) { btnRemovePress(mouseX, mouseY, mouseButton); }
});
main.addComponent(btnRemove);

// btnEdit
btnEditWidth = 16;
btnEditX = btnRemoveX - btnEditWidth - 1;
btnEdit = new Button(btnEditX, 0, Icons.CUT);
btnEdit.setEnabled(false);
btnEdit.setToolTip("Edit:", "Replace the selected paste with a different file.");
btnEdit.setClickListener(new ClickListener() {
@Override public void onClick(int mouseX, int mouseY, int mouseButton) { btnEditPress(mouseX, mouseY, mouseButton); }
});
main.addComponent(btnEdit);

// List of Pastes:
if(!pastes.isEmpty()){
itmPastes = new ItemList<Paste>(0, 17, width, 9, true);
for (Paste paste : pastes) {
itmPastes.addItem(paste);
}
itmPastes.setItemClickListener(new ItemClickListener<Paste>() {
@Override
public void onClick(Paste file, int index, int mouseButton) {
hasSelected = true;
btnRemove.setEnabled(true);
btnEdit.setEnabled(true);
}
});
main.addComponent(itmPastes);
}

this.setCurrentLayout(main);
}

public File openFile(){
File f = null;
private void btnRefreshPress(int mouseX, int mouseY, int mouseButton){
if(!pastes.isEmpty()){
pastes = server.getPastes(Minecraft.getMinecraft().player.getUniqueID().toString());
itmPastes.setItems(pastes);
}
}

private void btnEditPress(int mouseX, int mouseY, int mouseButton){
if(hasSelected){
server.editPaste(itmPastes.getSelectedItem().getId(), itmPastes.getSelectedItem());
}
}

private void btnRemovePress(int mouseX, int mouseY, int mouseButton){
if(hasSelected){
server.removePaste(itmPastes.getSelectedItem().user, itmPastes.getSelectedItem().getId());
pastes.remove(itmPastes.getSelectedItem());
itmPastes.removeItem(itmPastes.getSelectedIndex());
hasSelected = false;
btnRemove.setEnabled(false);
btnEdit.setEnabled(false);
}
}

private void btnImportPress(int mouseX, int mouseY, int mouseButton){

}

private void btnExportPress(int mouseX, int mouseY, int mouseButton){
Dialog.OpenFile dialog = new Dialog.OpenFile(App.this);
openDialog(dialog);
dialog.setResponseHandler(new Dialog.ResponseHandler<File>() {
@Override
public boolean onResponse(boolean success, File e) {
Paste p = new Paste(Minecraft.getMinecraft().player.getName(), e);
p.setId(server.postPaste(p));
Dialog.Confirmation confirm = new Dialog.Confirmation("Are you sure you want to delete " + p.getId() + "?");
openDialog(confirm);
confirm.setPositiveText("Removed!");
confirm.setPositiveListener(new ClickListener() {
@Override
public void onClick(int mouseX, int mouseY, int mouseButton) {
server.removePaste(p.user, p.getId());
}
});
return success;
public boolean onResponse(boolean success, File file) {
if(success){
Dialog.Confirmation confirm = new Dialog.Confirmation("Are you sure you want to upload " + file.getName() + "?");
openDialog(confirm);
confirm.setPositiveText("Uploaded!");
confirm.setPositiveListener(new ClickListener() {
@Override
public void onClick(int mouseX, int mouseY, int mouseButton) {
Paste p = new Paste(Minecraft.getMinecraft().player.getUniqueID().toString(), file);
p.setId(server.postPaste(p));
Dialog.Message showID = new Dialog.Message("Your paste id is: " + p.getId());
openDialog(showID);
dialog.close();
}
});
}
return false;
}
});
return f;
}

@Override
45 changes: 41 additions & 4 deletions src/main/java/com/cmdengineer/pastefile/utils/Paste.java
Original file line number Diff line number Diff line change
@@ -12,11 +12,13 @@ public class Paste {
public NBTTagCompound info;
public NBTTagCompound data;
private String id;
private File file;

public Paste(String user, File file){
this.user = user;
this.info = new NBTTagCompound();
this.data = new NBTTagCompound();
this.file = file;
if(file != null && !file.isFolder()){
info.setString("user", user);
info.setString("name", file.getName());
@@ -31,35 +33,51 @@ public Paste(String user, NBTTagCompound info, NBTTagCompound data){
this.info = info;
this.data = data;
this.user = user;
this.file = new File(this.info.getString("name"), this.info.getString("app"), this.data);
}

public Paste(NBTTagCompound info, NBTTagCompound data){
this.info = new NBTTagCompound();
this.data = new NBTTagCompound();
this.info = info;
this.data = data;
this.file = new File(this.info.getString("name"), this.info.getString("app"), this.data);
}

public Paste(String fromServer){
this.info = new NBTTagCompound();
this.data = new NBTTagCompound();
try {
String[] content = fromServer.split("<=Data=>");
System.out.println("INFO: " + content[0]);
System.out.println("DATA: " + content[1]);
this.data = JsonToNBT.getTagFromJson(content[1]);
this.info = JsonToNBT.getTagFromJson(content[0]);
this.file = new File(this.info.getString("name"), this.info.getString("app"), this.data);
} catch (NBTException e) {
e.printStackTrace();
}
}

public Paste(String info, String data){
public Paste(String user, String fromServer){
this.info = new NBTTagCompound();
this.data = new NBTTagCompound();
this.user = user;
try {
String[] content = fromServer.split("<=Data=>");
this.data = JsonToNBT.getTagFromJson(content[1]);
this.info = JsonToNBT.getTagFromJson(content[0]);
this.file = new File(this.info.getString("name"), this.info.getString("app"), this.data);
} catch (NBTException e) {
e.printStackTrace();
}
}

public Paste(int mode, String info, String data){
this.info = new NBTTagCompound();
this.data = new NBTTagCompound();
try {
this.data = JsonToNBT.getTagFromJson(data);
this.info = JsonToNBT.getTagFromJson(info);
this.file = new File(this.info.getString("name"), this.info.getString("app"), this.data);
} catch (NBTException e) {
e.printStackTrace();
}
@@ -68,21 +86,40 @@ public Paste(String info, String data){
public Paste(String user, String info, String data){
this.info = new NBTTagCompound();
this.data = new NBTTagCompound();
this.user = user;
try {
this.data = JsonToNBT.getTagFromJson(data);
this.info = JsonToNBT.getTagFromJson(info);
this.file = new File(this.info.getString("name"), this.info.getString("app"), this.data);
} catch (NBTException e) {
e.printStackTrace();
}
this.user = user;
}

@Override
public String toString() {
if(file != null && !file.isFolder()){
return file.getName();
}
if(info != null){
return info.getString("name");
}
return "Unknown.";
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public File getFile() {
return file;
}

public void setFile(File file) {
this.file = file;
}
}
Rate limit · GitHub

Access has been restricted

You have triggered a rate limit.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

0 comments on commit 48338ef

Please sign in to comment.