Right-click menu, Update core

This commit is contained in:
MrLetsplay 2023-12-03 21:33:20 +01:00
parent af0acb41d2
commit d37c71d208
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
7 changed files with 145 additions and 23 deletions

View File

@ -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

View File

@ -11,14 +11,14 @@
<command
categoryId="ShareClient.commands.category"
name="Sample Command"
id="ShareClient.commands.sampleCommand">
id="ShareClient.commands.shareProjectCommand">
</command>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
class="me.mrletsplay.shareclient.handlers.SampleHandler"
commandId="ShareClient.commands.sampleCommand">
class="me.mrletsplay.shareclient.handlers.ShareProjectHandler"
commandId="ShareClient.commands.shareProjectCommand">
</handler>
</extension>
<extension
@ -39,7 +39,7 @@
label="Sample Menu"
mnemonic="M">
<command
commandId="ShareClient.commands.sampleCommand"
commandId="ShareClient.commands.shareProjectCommand"
id="ShareClient.menus.sampleCommand"
mnemonic="S">
</command>
@ -51,12 +51,33 @@
id="ShareClient.toolbars.sampleToolbar">
<command
id="ShareClient.toolbars.sampleCommand"
commandId="ShareClient.commands.sampleCommand"
commandId="ShareClient.commands.shareProjectCommand"
icon="icons/sample.png"
tooltip="Say hello world">
</command>
</toolbar>
</menuContribution>
<menuContribution
allPopups="false"
locationURI="popup:org.eclipse.ui.popup.any?after=additions">
<menu
label="Share Client">
<command
commandId="ShareClient.commands.shareProjectCommand"
label="Share project"
style="push">
<visibleWhen
checkEnabled="false">
<iterate
ifEmpty="false">
<adapt
type="org.eclipse.core.resources.IResource">
</adapt>
</iterate>
</visibleWhen>
</command>
</menu>
</menuContribution>
</extension>
<extension
point="org.eclipse.ui.views">
@ -91,5 +112,13 @@
file="contexts.xml">
</contexts>
</extension>
<extension
point="org.eclipse.ui.preferencePages">
<page
class="me.mrletsplay.shareclient.ShareClientPreferencePage"
id="ShareClient.preferences"
name="Share Client">
</page>
</extension>
</plugin>

View File

@ -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;
}
}

View File

@ -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) {
}
}

View File

@ -0,0 +1,10 @@
package me.mrletsplay.shareclient;
public class ShareClientPreferences {
public static final String
SERVER_URI = "serverUri",
USERNAME = "username",
SHOW_CURSORS = "showCursors";
}

View File

@ -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());

View File

@ -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();
}
};