diff --git a/icons/cog.png b/icons/cog.png index 2e8f76f..e330165 100644 Binary files a/icons/cog.png and b/icons/cog.png differ diff --git a/icons/cog@2x.png b/icons/cog@2x.png index 43aa898..fe38695 100644 Binary files a/icons/cog@2x.png and b/icons/cog@2x.png differ diff --git a/icons/door.png b/icons/door.png new file mode 100644 index 0000000..5c060e9 Binary files /dev/null and b/icons/door.png differ diff --git a/icons/door@2x.png b/icons/door@2x.png new file mode 100644 index 0000000..cf217a2 Binary files /dev/null and b/icons/door@2x.png differ diff --git a/icons/icon.png b/icons/icon.png new file mode 100644 index 0000000..9b91d04 Binary files /dev/null and b/icons/icon.png differ diff --git a/icons/icon@2x.png b/icons/icon@2x.png new file mode 100644 index 0000000..f6ad71d Binary files /dev/null and b/icons/icon@2x.png differ diff --git a/icons/sample.png b/icons/sample.png deleted file mode 100644 index 02c4b79..0000000 Binary files a/icons/sample.png and /dev/null differ diff --git a/icons/sample@2x.png b/icons/sample@2x.png deleted file mode 100644 index c1224d1..0000000 Binary files a/icons/sample@2x.png and /dev/null differ diff --git a/icons/share.png b/icons/share.png new file mode 100644 index 0000000..25f3a80 Binary files /dev/null and b/icons/share.png differ diff --git a/icons/share@2x.png b/icons/share@2x.png new file mode 100644 index 0000000..dcabe61 Binary files /dev/null and b/icons/share@2x.png differ diff --git a/plugin.xml b/plugin.xml index 2d5e187..86b8195 100644 --- a/plugin.xml +++ b/plugin.xml @@ -32,38 +32,15 @@ - - - - - - - - - - - - diff --git a/src/main/java/me/mrletsplay/shareclient/Activator.java b/src/main/java/me/mrletsplay/shareclient/Activator.java index 69efa24..5a38e9c 100644 --- a/src/main/java/me/mrletsplay/shareclient/Activator.java +++ b/src/main/java/me/mrletsplay/shareclient/Activator.java @@ -56,25 +56,31 @@ public class Activator extends AbstractUIPlugin { return plugin; } + public RemoteConnection openConnection(String sessionID) { + String serverURI = getPreferenceStore().getString(ShareClientPreferences.SERVER_URI); + if(serverURI == null) return null; + + String username = getPreferenceStore().getString(ShareClientPreferences.USERNAME); + if(username == null || username.isBlank()) username = "user" + new Random().nextInt(1000); + + activeConnection = new WebSocketConnection(URI.create(serverURI), username); + try { + activeConnection.connect(sessionID); // 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 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; - } + openConnection(UUID.randomUUID().toString()); } return activeConnection; diff --git a/src/main/java/me/mrletsplay/shareclient/handlers/ShareProjectHandler.java b/src/main/java/me/mrletsplay/shareclient/handlers/ShareProjectHandler.java index 8144c45..07e41c0 100644 --- a/src/main/java/me/mrletsplay/shareclient/handlers/ShareProjectHandler.java +++ b/src/main/java/me/mrletsplay/shareclient/handlers/ShareProjectHandler.java @@ -1,7 +1,5 @@ package me.mrletsplay.shareclient.handlers; -import java.util.concurrent.atomic.AtomicBoolean; - import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; @@ -9,21 +7,12 @@ 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.document.DocumentListener; -import me.mrletsplay.shareclientcore.document.SharedDocument; public class ShareProjectHandler extends AbstractHandler { @@ -46,67 +35,67 @@ public class ShareProjectHandler extends AbstractHandler { RemoteConnection con = Activator.getDefault().getOrOpenConnection(); if(con == null) return null; - // TODO: share entire project + // TODO: handle case when adding project to existing session - IEditorPart editor = window.getActivePage().getActiveEditor(); - if(!(editor instanceof ITextEditor)) return null; - - ITextEditor textEditor = (ITextEditor) editor; - - IDocument eclipseDocument = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); - - AtomicBoolean ignoreChanges = new AtomicBoolean(false); - SharedDocument doc = new SharedDocument(con); - doc.localInsert(0, eclipseDocument.get()); - - doc.addListener(new DocumentListener() { - - @Override - public void onInsert(int index, char character) { - Display.getDefault().asyncExec(() -> { - try { - ignoreChanges.set(true); - eclipseDocument.replace(index, 0, String.valueOf(character)); - ignoreChanges.set(false); - } catch (BadLocationException e) { - e.printStackTrace(); - } - }); - - } - - @Override - public void onDelete(int index) { - Display.getDefault().asyncExec(() -> { - try { - ignoreChanges.set(true); - eclipseDocument.replace(index, 1, ""); - ignoreChanges.set(false); - } catch (BadLocationException e) { - e.printStackTrace(); - } - }); - } - }); - - eclipseDocument.addDocumentListener(new IDocumentListener() { - - @Override - public void documentChanged(DocumentEvent event) { - if(ignoreChanges.get()) return; // TODO: not very ideal - - if(event.getLength() > 0) { - doc.localDelete(event.getOffset(), event.getLength()); - } - - doc.localInsert(event.getOffset(), event.getText()); - } - - @Override - public void documentAboutToBeChanged(DocumentEvent event) { - - } - }); +// IEditorPart editor = window.getActivePage().getActiveEditor(); +// if(!(editor instanceof ITextEditor)) return null; +// +// ITextEditor textEditor = (ITextEditor) editor; +// +// IDocument eclipseDocument = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); +// +// AtomicBoolean ignoreChanges = new AtomicBoolean(false); +// SharedDocument doc = new SharedDocument(con); +// doc.localInsert(0, eclipseDocument.get()); +// +// doc.addListener(new DocumentListener() { +// +// @Override +// public void onInsert(int index, char character) { +// Display.getDefault().asyncExec(() -> { +// try { +// ignoreChanges.set(true); +// eclipseDocument.replace(index, 0, String.valueOf(character)); +// ignoreChanges.set(false); +// } catch (BadLocationException e) { +// e.printStackTrace(); +// } +// }); +// +// } +// +// @Override +// public void onDelete(int index) { +// Display.getDefault().asyncExec(() -> { +// try { +// ignoreChanges.set(true); +// eclipseDocument.replace(index, 1, ""); +// ignoreChanges.set(false); +// } catch (BadLocationException e) { +// e.printStackTrace(); +// } +// }); +// } +// }); +// +// eclipseDocument.addDocumentListener(new IDocumentListener() { +// +// @Override +// public void documentChanged(DocumentEvent event) { +// if(ignoreChanges.get()) return; // TODO: not very ideal +// +// if(event.getLength() > 0) { +// doc.localDelete(event.getOffset(), event.getLength()); +// } +// +// doc.localInsert(event.getOffset(), event.getText()); +// } +// +// @Override +// public void documentAboutToBeChanged(DocumentEvent event) { +// +// } +// }); return null; } diff --git a/src/main/java/me/mrletsplay/shareclient/views/ShareView.java b/src/main/java/me/mrletsplay/shareclient/views/ShareView.java index 42b829b..33b2695 100644 --- a/src/main/java/me/mrletsplay/shareclient/views/ShareView.java +++ b/src/main/java/me/mrletsplay/shareclient/views/ShareView.java @@ -1,9 +1,14 @@ package me.mrletsplay.shareclient.views; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + import javax.inject.Inject; import org.eclipse.jface.action.Action; +import org.eclipse.jface.dialogs.InputDialog; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.preference.PreferenceDialog; import org.eclipse.jface.resource.ImageDescriptor; @@ -15,30 +20,18 @@ import org.eclipse.jface.viewers.TableViewer; import org.eclipse.swt.SWT; 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; - -/** - * This sample class demonstrates how to plug-in a new - * workbench view. The view shows data obtained from the - * model. The sample creates a dummy model on the fly, - * but a real implementation would connect to the model - * available either in this or another plug-in (e.g. the workspace). - * The view is connected to the model using a content provider. - *

- * The view uses a label provider to define how model - * objects should be presented in the view. Each - * view can present the same model objects using - * different labels and icons, if needed. Alternatively, - * a single label provider can be shared between views - * in order to ensure that objects of the same type are - * presented in the same way everywhere. - *

- */ +import me.mrletsplay.shareclient.Activator; +import me.mrletsplay.shareclientcore.connection.ConnectionException; +import me.mrletsplay.shareclientcore.connection.RemoteConnection; +import me.mrletsplay.shareclientcore.connection.message.PeerJoinMessage; +import me.mrletsplay.shareclientcore.connection.message.RequestFullSyncMessage; public class ShareView extends ViewPart { @@ -51,6 +44,8 @@ public class ShareView extends ViewPart { private TableViewer viewer; + private List peerNames = new ArrayList<>(); + class ViewLabelProvider extends LabelProvider implements ITableLabelProvider { @Override public String getColumnText(Object obj, int index) { @@ -71,7 +66,7 @@ public class ShareView extends ViewPart { viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); viewer.setContentProvider(ArrayContentProvider.getInstance()); - viewer.setInput(new String[] { "One", "Two", "Three" }); + viewer.setInput(new String[] {}); viewer.setLabelProvider(new ViewLabelProvider()); // Create the help context id for the viewer's control @@ -80,7 +75,42 @@ public class ShareView extends ViewPart { IActionBars bars = getViewSite().getActionBars(); - Action showSettings = new Action() { + Action joinSession = new Action("Join session", ImageDescriptor.createFromFile(ShareView.class, "/icons/door.png")) { + + @Override + public void run() { + InputDialog input = new InputDialog(viewer.getControl().getShell(), "Join session", "Enter session id", "EEE", null); + input.setBlockOnOpen(true); + if(input.open() != InputDialog.OK) return; + + RemoteConnection connection = Activator.getDefault().getActiveConnection(); + if(connection != null) connection.disconnect(); + + connection = Activator.getDefault().openConnection(input.getValue()); + if(connection == null) return; + + connection.addListener(m -> { + System.out.println("Got: " + m); + if(m instanceof PeerJoinMessage join) { + peerNames.add(join.peerName()); + Display.getDefault().asyncExec(() -> viewer.setInput(peerNames.toArray(String[]::new))); + } + + // TODO: handle FULL_SYNC + }); + + try { + connection.send(new RequestFullSyncMessage(connection.getSiteID(), null)); + } catch (ConnectionException e) { + connection.disconnect(); + showMessage("Failed to send: " + e); + } + } + + }; + bars.getToolBarManager().add(joinSession); + + Action showSettings = new Action("Settings", ImageDescriptor.createFromFile(ShareView.class, "/icons/cog.png")) { @Override public void run() { @@ -91,20 +121,17 @@ public class ShareView extends ViewPart { } }; + bars.getToolBarManager().add(showSettings); viewer.addDoubleClickListener(event -> { - showMessage(String.valueOf(((StructuredSelection) event.getSelection()).toArray()[0])); + showMessage(Arrays.toString(((StructuredSelection) event.getSelection()).toArray())); }); - - showSettings.setImageDescriptor(ImageDescriptor.createFromFile(ShareView.class, "/icons/cog.png")); - - bars.getToolBarManager().add(showSettings); } private void showMessage(String message) { MessageDialog.openInformation( viewer.getControl().getShell(), - "Sample View", + "Share Client", message); }