From d37c71d20878152f241340b1efb3fffd2c3b7317 Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Sun, 3 Dec 2023 21:33:20 +0100 Subject: [PATCH] Right-click menu, Update core --- META-INF/MANIFEST.MF | 3 +- plugin.xml | 39 +++++++++++++++--- .../me/mrletsplay/shareclient/Activator.java | 40 +++++++++++++++++++ .../ShareClientPreferencePage.java | 29 ++++++++++++++ .../shareclient/ShareClientPreferences.java | 10 +++++ ...eHandler.java => ShareProjectHandler.java} | 40 +++++++++++-------- .../shareclient/views/ShareView.java | 7 +++- 7 files changed, 145 insertions(+), 23 deletions(-) create mode 100644 src/main/java/me/mrletsplay/shareclient/ShareClientPreferencePage.java create mode 100644 src/main/java/me/mrletsplay/shareclient/ShareClientPreferences.java rename src/main/java/me/mrletsplay/shareclient/handlers/{SampleHandler.java => ShareProjectHandler.java} (75%) diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF index f206159..9e50dea 100644 --- a/META-INF/MANIFEST.MF +++ b/META-INF/MANIFEST.MF @@ -10,7 +10,8 @@ Require-Bundle: org.eclipse.ui, org.eclipse.ui.editors;bundle-version="3.17.0", org.eclipse.text, Java-WebSocket;bundle-version="1.5.4", - wrapped.me.mrletsplay.ShareClient-Core;bundle-version="1.0.0" + wrapped.me.mrletsplay.ShareClient-Core;bundle-version="1.0.0", + org.eclipse.core.resources;bundle-version="3.19.100" Bundle-RequiredExecutionEnvironment: JavaSE-17 Automatic-Module-Name: ShareClient Bundle-ActivationPolicy: lazy diff --git a/plugin.xml b/plugin.xml index 066680c..2d5e187 100644 --- a/plugin.xml +++ b/plugin.xml @@ -11,14 +11,14 @@ + id="ShareClient.commands.shareProjectCommand"> + class="me.mrletsplay.shareclient.handlers.ShareProjectHandler" + commandId="ShareClient.commands.shareProjectCommand"> @@ -51,12 +51,33 @@ id="ShareClient.toolbars.sampleToolbar"> + + + + + + + + + + + + @@ -91,5 +112,13 @@ file="contexts.xml"> + + + + diff --git a/src/main/java/me/mrletsplay/shareclient/Activator.java b/src/main/java/me/mrletsplay/shareclient/Activator.java index 1a04c98..69efa24 100644 --- a/src/main/java/me/mrletsplay/shareclient/Activator.java +++ b/src/main/java/me/mrletsplay/shareclient/Activator.java @@ -1,8 +1,17 @@ package me.mrletsplay.shareclient; +import java.net.URI; +import java.util.Random; +import java.util.UUID; + +import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.ui.plugin.AbstractUIPlugin; import org.osgi.framework.BundleContext; +import me.mrletsplay.shareclientcore.connection.ConnectionException; +import me.mrletsplay.shareclientcore.connection.RemoteConnection; +import me.mrletsplay.shareclientcore.connection.WebSocketConnection; + /** * The activator class controls the plug-in life cycle */ @@ -14,6 +23,8 @@ public class Activator extends AbstractUIPlugin { // The shared instance private static Activator plugin; + private RemoteConnection activeConnection; + /** * The constructor */ @@ -26,6 +37,7 @@ public class Activator extends AbstractUIPlugin { plugin = this; System.out.println("STARTING"); + getPreferenceStore().setDefault(ShareClientPreferences.SERVER_URI, "ws://localhost:5473"); // new ShareWSClient(URI.create("ws://localhost:5473")).connect(); } @@ -44,4 +56,32 @@ public class Activator extends AbstractUIPlugin { return plugin; } + public RemoteConnection getOrOpenConnection() { + if(activeConnection == null) { + String serverURI = getPreferenceStore().getString(ShareClientPreferences.SERVER_URI); + if(serverURI == null) return null; + + String username = getPreferenceStore().getString(ShareClientPreferences.USERNAME); + if(username == null) username = "user" + new Random().nextInt(1000); + + activeConnection = new WebSocketConnection(URI.create(serverURI), username); + try { + activeConnection.connect(UUID.randomUUID().toString()); // TODO: connect to existing session + } catch (ConnectionException e) { + MessageDialog.openInformation( + null, + "Share Client", + "Failed to connect to server: " + e); + activeConnection = null; + return null; + } + } + + return activeConnection; + } + + public RemoteConnection getActiveConnection() { + return activeConnection; + } + } diff --git a/src/main/java/me/mrletsplay/shareclient/ShareClientPreferencePage.java b/src/main/java/me/mrletsplay/shareclient/ShareClientPreferencePage.java new file mode 100644 index 0000000..b0aa9fa --- /dev/null +++ b/src/main/java/me/mrletsplay/shareclient/ShareClientPreferencePage.java @@ -0,0 +1,29 @@ +package me.mrletsplay.shareclient; + +import org.eclipse.jface.preference.BooleanFieldEditor; +import org.eclipse.jface.preference.FieldEditorPreferencePage; +import org.eclipse.jface.preference.StringFieldEditor; +import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.IWorkbenchPreferencePage; + +public class ShareClientPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage { + + public ShareClientPreferencePage() { +// super(GRID); + setPreferenceStore(Activator.getDefault().getPreferenceStore()); + setDescription("Share Client Preferences"); + } + + @Override + protected void createFieldEditors() { + addField(new StringFieldEditor(ShareClientPreferences.SERVER_URI, "Server URI", getFieldEditorParent())); + addField(new StringFieldEditor(ShareClientPreferences.USERNAME, "Display Name", getFieldEditorParent())); + addField(new BooleanFieldEditor(ShareClientPreferences.SHOW_CURSORS, "Show other users' cursors", getFieldEditorParent())); + } + + @Override + public void init(IWorkbench workbench) { + + } + +} diff --git a/src/main/java/me/mrletsplay/shareclient/ShareClientPreferences.java b/src/main/java/me/mrletsplay/shareclient/ShareClientPreferences.java new file mode 100644 index 0000000..a2a4e08 --- /dev/null +++ b/src/main/java/me/mrletsplay/shareclient/ShareClientPreferences.java @@ -0,0 +1,10 @@ +package me.mrletsplay.shareclient; + +public class ShareClientPreferences { + + public static final String + SERVER_URI = "serverUri", + USERNAME = "username", + SHOW_CURSORS = "showCursors"; + +} diff --git a/src/main/java/me/mrletsplay/shareclient/handlers/SampleHandler.java b/src/main/java/me/mrletsplay/shareclient/handlers/ShareProjectHandler.java similarity index 75% rename from src/main/java/me/mrletsplay/shareclient/handlers/SampleHandler.java rename to src/main/java/me/mrletsplay/shareclient/handlers/ShareProjectHandler.java index b809884..8144c45 100644 --- a/src/main/java/me/mrletsplay/shareclient/handlers/SampleHandler.java +++ b/src/main/java/me/mrletsplay/shareclient/handlers/ShareProjectHandler.java @@ -1,37 +1,52 @@ package me.mrletsplay.shareclient.handlers; -import java.io.IOException; -import java.net.URI; import java.util.concurrent.atomic.AtomicBoolean; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; -import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.Adapters; +import org.eclipse.core.runtime.IPath; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.DocumentEvent; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IDocumentListener; +import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.handlers.HandlerUtil; import org.eclipse.ui.texteditor.ITextEditor; +import me.mrletsplay.shareclient.Activator; import me.mrletsplay.shareclientcore.connection.RemoteConnection; -import me.mrletsplay.shareclientcore.connection.WebSocketConnection; import me.mrletsplay.shareclientcore.document.DocumentListener; import me.mrletsplay.shareclientcore.document.SharedDocument; -public class SampleHandler extends AbstractHandler { +public class ShareProjectHandler extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event); - MessageDialog.openInformation( - window.getShell(), - "ShareClient", - "Hello, Eclipse world"); + + IStructuredSelection selection = HandlerUtil.getCurrentStructuredSelection(event); + if(selection.isEmpty()) return null; + + IResource res = Adapters.adapt(selection.getFirstElement(), IResource.class, false); + if(res == null) return null; + + IProject project = res.getProject(); + if(project == null) return null; + + IPath path = project.getLocation(); + if(path == null) return null; + + RemoteConnection con = Activator.getDefault().getOrOpenConnection(); + if(con == null) return null; + + // TODO: share entire project IEditorPart editor = window.getActivePage().getActiveEditor(); if(!(editor instanceof ITextEditor)) return null; @@ -40,13 +55,6 @@ public class SampleHandler extends AbstractHandler { IDocument eclipseDocument = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); - RemoteConnection con = new WebSocketConnection(URI.create("ws://localhost:5473")); - try { - con.connect(); - } catch (IOException | InterruptedException e) { - e.printStackTrace(); - } - AtomicBoolean ignoreChanges = new AtomicBoolean(false); SharedDocument doc = new SharedDocument(con); doc.localInsert(0, eclipseDocument.get()); diff --git a/src/main/java/me/mrletsplay/shareclient/views/ShareView.java b/src/main/java/me/mrletsplay/shareclient/views/ShareView.java index 052a384..42b829b 100644 --- a/src/main/java/me/mrletsplay/shareclient/views/ShareView.java +++ b/src/main/java/me/mrletsplay/shareclient/views/ShareView.java @@ -5,6 +5,7 @@ import javax.inject.Inject; import org.eclipse.jface.action.Action; import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.preference.PreferenceDialog; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.viewers.ArrayContentProvider; import org.eclipse.jface.viewers.ITableLabelProvider; @@ -17,6 +18,7 @@ import org.eclipse.swt.widgets.Composite; 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; @@ -82,7 +84,10 @@ public class ShareView extends ViewPart { @Override public void run() { - showMessage("Settings"); + PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn( + null, "ShareClient.preferences", + null, null); + dialog.open(); } };