From fa4b90bd72832e685538be2896735b40a48909a4 Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Wed, 6 Dec 2023 21:07:03 +0100 Subject: [PATCH] Update toolbar, Sync testing --- icons/account.png | Bin 0 -> 386 bytes icons/account@2x.png | Bin 0 -> 568 bytes icons/stop.png | Bin 0 -> 162 bytes icons/stop@2x.png | Bin 0 -> 187 bytes .../me/mrletsplay/shareclient/Activator.java | 6 ++ .../shareclient/util/ProjectRelativePath.java | 16 ++++ .../shareclient/views/ShareView.java | 73 +++++++++++++++--- 7 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 icons/account.png create mode 100644 icons/account@2x.png create mode 100644 icons/stop.png create mode 100644 icons/stop@2x.png create mode 100644 src/main/java/me/mrletsplay/shareclient/util/ProjectRelativePath.java diff --git a/icons/account.png b/icons/account.png new file mode 100644 index 0000000000000000000000000000000000000000..7485660d8f0162aa00a338582a1803df5f4e0961 GIT binary patch literal 386 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`oCO|{#X#yh2s3WX6WN&PIxXfsxCCYF{NKg|NC zr8Gr;-d*|m-t7PP(>rDwF4@MmlJUg4Zk^L9^PamiJg_*o`1P)MGr5~?9ES^3S}GU{ zBAoq0L(euV{KzO;tIVOOckKv+!Q>#loIC4y%4Rbdzt2r7xX;p&k*OSI%+O%br_VU& z`lIagpVyfgy@+0Gx-4^7tjJMAL0^V9S3XD!?v+S>x8>UWxw0;SC0;J(498^o_c2?1 zuKm9)b%M&tmYMSzFUZFl8~bm)w*J5^u^z+UwntW2vfH=c=a~|;dXtWSm4t)l)~wzS e_oe}M{P)c(14-oMOM3^Fz8(6do_$!3PDx^_Fn9M~+ z!PaVf3GA+=mA#FiMnqH0Jr)}*mSl2A?iz)hZti*Kybs(t=ZvT->x|l3-8TUL0>G(1 zc`48HGqc$&7Lls}B63)-*I&kQ{Hba!soDkO@%SF_3}_!qeHD?NBuQSM7=X3*j&tq} z5G|1us(RNLv*%m0$~PdYIxH~2O0SX+d>QiIPtNHVZ0tAi!ZyAk7qtoqn`=W@P16uRr@2dK7I-PzP z3;IGB9IWQ6N{;Bbe$)dx}DBb|99pD$?$E2|_z-}1;0000N&PEETNZ-@NF~p)b`Op9V_ROjcol19` z4>5?9G`4b!KjNM8<41+ts$B_J8V)8fJkE$qU|`TY$tC^c-1AL9tqh*7elF{r5}E+| CoivdE literal 0 HcmV?d00001 diff --git a/icons/stop@2x.png b/icons/stop@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..197c8e7a894d6afb2d755bb100ee1a1b65b138a7 GIT binary patch literal 187 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz&H|6fVg?4`^&rfc{H*UVP*AeO zHKHUqKdq!Zu_%?Hyu4g5GcUV1Ik6yBFTW^#_B$IXpdt@X7srr@*0WoL}E~fqKE9X+Hx)!+S>N XH~YD)eSMd;f&@KX{an^LB{Ts5@eny+ literal 0 HcmV?d00001 diff --git a/src/main/java/me/mrletsplay/shareclient/Activator.java b/src/main/java/me/mrletsplay/shareclient/Activator.java index 5a38e9c..5ab4682 100644 --- a/src/main/java/me/mrletsplay/shareclient/Activator.java +++ b/src/main/java/me/mrletsplay/shareclient/Activator.java @@ -86,6 +86,12 @@ public class Activator extends AbstractUIPlugin { return activeConnection; } + public void closeConnection() { + if(activeConnection == null) return; + activeConnection.disconnect(); + activeConnection = null; + } + public RemoteConnection getActiveConnection() { return activeConnection; } diff --git a/src/main/java/me/mrletsplay/shareclient/util/ProjectRelativePath.java b/src/main/java/me/mrletsplay/shareclient/util/ProjectRelativePath.java new file mode 100644 index 0000000..bbaffbf --- /dev/null +++ b/src/main/java/me/mrletsplay/shareclient/util/ProjectRelativePath.java @@ -0,0 +1,16 @@ +package me.mrletsplay.shareclient.util; + +public record ProjectRelativePath(String projectName, String relativePath) { + + @Override + public String toString() { + return projectName + ":" + relativePath; + } + + public static ProjectRelativePath of(String raw) { + String[] spl = raw.split(":", 2); + if(spl.length != 2) throw new IllegalArgumentException("Invalid path"); + return new ProjectRelativePath(spl[0], spl[1]); + } + +} diff --git a/src/main/java/me/mrletsplay/shareclient/views/ShareView.java b/src/main/java/me/mrletsplay/shareclient/views/ShareView.java index 33b2695..adae457 100644 --- a/src/main/java/me/mrletsplay/shareclient/views/ShareView.java +++ b/src/main/java/me/mrletsplay/shareclient/views/ShareView.java @@ -1,13 +1,19 @@ package me.mrletsplay.shareclient.views; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javax.inject.Inject; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.jface.action.Action; +import org.eclipse.jface.action.IToolBarManager; import org.eclipse.jface.dialogs.InputDialog; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.preference.PreferenceDialog; @@ -22,14 +28,15 @@ import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IActionBars; -import org.eclipse.ui.ISharedImages; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.dialogs.PreferencesUtil; import org.eclipse.ui.part.ViewPart; import me.mrletsplay.shareclient.Activator; +import me.mrletsplay.shareclient.util.ProjectRelativePath; import me.mrletsplay.shareclientcore.connection.ConnectionException; import me.mrletsplay.shareclientcore.connection.RemoteConnection; +import me.mrletsplay.shareclientcore.connection.message.FullSyncMessage; import me.mrletsplay.shareclientcore.connection.message.PeerJoinMessage; import me.mrletsplay.shareclientcore.connection.message.RequestFullSyncMessage; @@ -57,7 +64,7 @@ public class ShareView extends ViewPart { } @Override public Image getImage(Object obj) { - return workbench.getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT); + return ImageDescriptor.createFromFile(ShareView.class, "/icons/account.png").createImage(); } } @@ -73,12 +80,25 @@ public class ShareView extends ViewPart { workbench.getHelpSystem().setHelp(viewer.getControl(), "ShareClient.viewer"); getSite().setSelectionProvider(viewer); + viewer.addDoubleClickListener(event -> { + showMessage(Arrays.toString(((StructuredSelection) event.getSelection()).toArray())); + }); + + updateActionBars(); + } + + private void updateActionBars() { IActionBars bars = getViewSite().getActionBars(); + IToolBarManager toolbars = bars.getToolBarManager(); + + toolbars.removeAll(); Action joinSession = new Action("Join session", ImageDescriptor.createFromFile(ShareView.class, "/icons/door.png")) { @Override public void run() { + showMessage(Arrays.stream(ResourcesPlugin.getWorkspace().getRoot().getProjects()).map(p -> p.getName() + ": " + p.getLocation().toString()).toList().toString()); + InputDialog input = new InputDialog(viewer.getControl().getShell(), "Join session", "Enter session id", "EEE", null); input.setBlockOnOpen(true); if(input.open() != InputDialog.OK) return; @@ -96,19 +116,56 @@ public class ShareView extends ViewPart { Display.getDefault().asyncExec(() -> viewer.setInput(peerNames.toArray(String[]::new))); } - // TODO: handle FULL_SYNC + if(m instanceof FullSyncMessage sync) { + // TODO: handle FULL_SYNC + ProjectRelativePath path; + try { + path = ProjectRelativePath.of(sync.documentPath()); + }catch(IllegalArgumentException e) { + return; + } + + IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(path.projectName()); + if(project == null) return; // TODO: create project + + Path filePath = project.getLocation().toPath().resolve(path.relativePath()); + try { + if(!Files.exists(filePath)) { + Files.createDirectories(filePath.getParent()); + Files.createFile(filePath); + } + + Files.write(filePath, sync.content()); + } catch (IOException e) { + // TODO: handle exception + } + } }); try { connection.send(new RequestFullSyncMessage(connection.getSiteID(), null)); + updateActionBars(); } catch (ConnectionException e) { - connection.disconnect(); + Activator.getDefault().closeConnection(); showMessage("Failed to send: " + e); } } }; - bars.getToolBarManager().add(joinSession); + if(Activator.getDefault().getActiveConnection() == null) toolbars.add(joinSession); + + Action leaveSession = new Action("Leave session", ImageDescriptor.createFromFile(ShareView.class, "/icons/stop.png")) { + + @Override + public void run() { + Activator.getDefault().closeConnection(); + peerNames.clear(); + viewer.setInput(peerNames.toArray(String[]::new)); + updateActionBars(); + } + + }; + if(Activator.getDefault().getActiveConnection() != null) toolbars.add(leaveSession); Action showSettings = new Action("Settings", ImageDescriptor.createFromFile(ShareView.class, "/icons/cog.png")) { @@ -121,11 +178,9 @@ public class ShareView extends ViewPart { } }; - bars.getToolBarManager().add(showSettings); + toolbars.add(showSettings); - viewer.addDoubleClickListener(event -> { - showMessage(Arrays.toString(((StructuredSelection) event.getSelection()).toArray())); - }); + bars.updateActionBars(); } private void showMessage(String message) {