From 81a0ff1d44627f308bd1fe19c9f7fa8129f0f035 Mon Sep 17 00:00:00 2001 From: Branislav Stojkovic Date: Sun, 23 Oct 2016 17:06:28 +0200 Subject: [PATCH] download and open files from the cloud --- .../diylc/plugins/cloud/model/ServiceAPI.java | 2 +- .../cloud/presenter/CloudPresenter.java | 8 + .../src/org/diylc/swing/ActionFactory.java | 8 +- .../src/org/diylc/swing/ISwingUI.java | 17 +- .../org/diylc/swing/gui/DialogFactory.java | 15 +- .../src/org/diylc/swing/gui/MainFrame.java | 28 +-- .../swing/plugins/cloud/CloudPlugIn.java | 20 ++- .../cloud/view/ChangePasswordDialog.java | 100 +++++------ .../plugins/cloud/view/CloudBrowserFrame.java | 164 +++++++++++++----- .../plugins/cloud/view/UploadDialog.java | 38 ++-- .../plugins/cloud/view/UserEditDialog.java | 154 ++++++++-------- .../diylc/swing/plugins/file/BomDialog.java | 21 ++- 12 files changed, 329 insertions(+), 246 deletions(-) diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/plugins/cloud/model/ServiceAPI.java b/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/plugins/cloud/model/ServiceAPI.java index a82f085b..8e5729f1 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/plugins/cloud/model/ServiceAPI.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/plugins/cloud/model/ServiceAPI.java @@ -158,5 +158,5 @@ public interface ServiceAPI { /** * @return a {@link List} of available sortings. */ - List getAvailableSortings(); + List getSortings(); } diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/plugins/cloud/presenter/CloudPresenter.java b/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/plugins/cloud/presenter/CloudPresenter.java index 3235e03d..3cc31ab1 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/plugins/cloud/presenter/CloudPresenter.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/plugins/cloud/presenter/CloudPresenter.java @@ -247,6 +247,14 @@ public class CloudPresenter { throw new CloudException(e); } } + + public String[] getSortings() throws CloudException { + try { + return service.getSortings().toArray(new String[0]); + } catch (Exception e) { + throw new CloudException(e); + } + } private String getMachineId() { if (machineId == null) { diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/ActionFactory.java b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/ActionFactory.java index 849e3531..65f484a9 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/ActionFactory.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/ActionFactory.java @@ -381,7 +381,7 @@ public class ActionFactory { public void actionPerformed(ActionEvent e) { LOG.info("SaveAction triggered"); if (plugInPort.getCurrentFileName() == null) { - final File file = DialogFactory.getInstance().showSaveDialog( + final File file = DialogFactory.getInstance().showSaveDialog(swingUI.getOwnerFrame(), FileFilterEnum.DIY.getFilter(), null, FileFilterEnum.DIY.getExtensions()[0], null); if (file != null) { @@ -455,7 +455,7 @@ public class ActionFactory { @Override public void actionPerformed(ActionEvent e) { LOG.info("SaveAsAction triggered"); - final File file = DialogFactory.getInstance().showSaveDialog( + final File file = DialogFactory.getInstance().showSaveDialog(swingUI.getOwnerFrame(), FileFilterEnum.DIY.getFilter(), null, FileFilterEnum.DIY.getExtensions()[0], null); if (file != null) { @@ -526,7 +526,7 @@ public class ActionFactory { @Override public void actionPerformed(ActionEvent e) { LOG.info("ExportPDFAction triggered"); - final File file = DialogFactory.getInstance().showSaveDialog( + final File file = DialogFactory.getInstance().showSaveDialog(swingUI.getOwnerFrame(), FileFilterEnum.PDF.getFilter(), null, FileFilterEnum.PDF.getExtensions()[0], null); if (file != null) { @@ -574,7 +574,7 @@ public class ActionFactory { @Override public void actionPerformed(ActionEvent e) { LOG.info("ExportPNGAction triggered"); - final File file = DialogFactory.getInstance().showSaveDialog( + final File file = DialogFactory.getInstance().showSaveDialog(swingUI.getOwnerFrame(), FileFilterEnum.PNG.getFilter(), null, FileFilterEnum.PNG.getExtensions()[0], null); if (file != null) { diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/ISwingUI.java b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/ISwingUI.java index b316113f..d47f195b 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/ISwingUI.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/ISwingUI.java @@ -3,6 +3,7 @@ package org.diylc.swing; import javax.swing.Action; import javax.swing.Icon; import javax.swing.JComponent; +import javax.swing.JFrame; import javax.swing.SwingConstants; import javax.swing.JPopupMenu.Separator; @@ -35,7 +36,8 @@ public interface ISwingUI extends IView { * @throws BadPositionException * in case invalid position is specified */ - void injectGUIComponent(JComponent component, int position) throws BadPositionException; + void injectGUIComponent(JComponent component, int position) + throws BadPositionException; /** * Injects a custom menu action into application's main menu. If @@ -59,6 +61,17 @@ public interface ISwingUI extends IView { * @param parentMenuName */ void injectSubmenu(String name, Icon icon, String parentMenuName); - + + /** + * Runs a task in background while showing busy cursor and a glass pane. + * + * @param task + */ void executeBackgroundTask(ITask task); + + /** + * @return {@link JFrame} that can be used to reference secondary dialogs + * and frames + */ + JFrame getOwnerFrame(); } diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/gui/DialogFactory.java b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/gui/DialogFactory.java index 2debd119..39766630 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/gui/DialogFactory.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/gui/DialogFactory.java @@ -1,5 +1,6 @@ package org.diylc.swing.gui; +import java.awt.Window; import java.io.File; import java.util.List; @@ -16,10 +17,9 @@ import org.diylc.plugins.cloud.presenter.CloudPresenter; import org.diylc.swing.gui.components.OverwritePromptFileChooser; import org.diylc.swing.gui.editor.PropertyEditorDialog; import org.diylc.swing.plugins.cloud.view.ChangePasswordDialog; -import org.diylc.swing.plugins.cloud.view.CloudBrowserFrame; import org.diylc.swing.plugins.cloud.view.LoginDialog; -import org.diylc.swing.plugins.cloud.view.UserEditDialog; import org.diylc.swing.plugins.cloud.view.UploadDialog; +import org.diylc.swing.plugins.cloud.view.UserEditDialog; import org.diylc.swing.plugins.file.BomDialog; import org.diylc.swingframework.AboutDialog; import org.diylc.swingframework.IFileChooserAccessory; @@ -84,13 +84,13 @@ public class DialogFactory { defaultExtension); } - public File showSaveDialog(FileFilter fileFilter, File initialFile, + public File showSaveDialog(Window owner, FileFilter fileFilter, File initialFile, String defaultExtension, IFileChooserAccessory accessory) { JFileChooser saveFileChooser = new OverwritePromptFileChooser(); initializeFileChooser(saveFileChooser, fileFilter, initialFile, defaultExtension, accessory); - int result = saveFileChooser.showSaveDialog(mainFrame); + int result = saveFileChooser.showSaveDialog(owner); return processFileChooserResult(result, saveFileChooser, defaultExtension); @@ -165,13 +165,6 @@ public class DialogFactory { return dialog; } - public CloudBrowserFrame createCloudBrowserFrame(IPlugInPort plugInPort, - CloudPresenter cloudPresenter) { - CloudBrowserFrame frame = new CloudBrowserFrame(mainFrame, plugInPort, - cloudPresenter); - return frame; - } - public ProgressDialog createProgressDialog(String title, String[] buttonCaptions, String description, boolean useProgress) { ProgressDialog dialog = new ProgressDialog(mainFrame, title, diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/gui/MainFrame.java b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/gui/MainFrame.java index 07608baa..ec12ea89 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/gui/MainFrame.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/gui/MainFrame.java @@ -88,8 +88,8 @@ public class MainFrame extends JFrame implements ISwingUI { createBasePanels(); menuMap = new HashMap(); buttonGroupMap = new HashMap(); - setIconImages(Arrays.asList(IconLoader.IconSmall.getImage(), IconLoader.IconMedium - .getImage(), IconLoader.IconLarge.getImage())); + setIconImages(Arrays.asList(IconLoader.IconSmall.getImage(), + IconLoader.IconMedium.getImage(), IconLoader.IconLarge.getImage())); DialogFactory.getInstance().initialize(this); this.presenter = new Presenter(this); @@ -176,7 +176,7 @@ public class MainFrame extends JFrame implements ISwingUI { // SwingUtilities.invokeLater(new Runnable() { // @Override // public void run() { - // + // // } // }); // } @@ -278,8 +278,8 @@ public class MainFrame extends JFrame implements ISwingUI { @Override public void injectMenuAction(Action action, String menuName) { - LOG.info(String.format("injectMenuAction(%s, %s)", action == null ? "Separator" : action - .getValue(Action.NAME), menuName)); + LOG.info(String.format("injectMenuAction(%s, %s)", + action == null ? "Separator" : action.getValue(Action.NAME), menuName)); JMenu menu = findOrCreateMenu(menuName); if (action == null) { menu.addSeparator(); @@ -314,7 +314,7 @@ public class MainFrame extends JFrame implements ISwingUI { menu.add(submenu); menuMap.put(name, submenu); } - + @Override public void executeBackgroundTask(final ITask task) { getGlassPane().setVisible(true); @@ -342,12 +342,16 @@ public class MainFrame extends JFrame implements ISwingUI { }; worker.execute(); } - + + @Override + public JFrame getOwnerFrame() { + return this; + } + @Override public File promptFileSave() { - return DialogFactory.getInstance().showSaveDialog( - FileFilterEnum.DIY.getFilter(), null, - FileFilterEnum.DIY.getExtensions()[0], null); + return DialogFactory.getInstance().showSaveDialog(this, FileFilterEnum.DIY.getFilter(), + null, FileFilterEnum.DIY.getExtensions()[0], null); } class FramePlugin implements IPlugIn { @@ -372,8 +376,8 @@ public class MainFrame extends JFrame implements ISwingUI { fileName = "Untitled"; } String modified = (Boolean) params[1] ? " (modified)" : ""; - setTitle(String.format("DIYLC %s - %s %s", plugInPort - .getCurrentVersionNumber(), fileName, modified)); + setTitle(String.format("DIYLC %s - %s %s", plugInPort.getCurrentVersionNumber(), + fileName, modified)); } } } diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/CloudPlugIn.java b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/CloudPlugIn.java index 6b245f12..7ca42c76 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/CloudPlugIn.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/CloudPlugIn.java @@ -14,7 +14,6 @@ import org.diylc.common.IPlugInPort; import org.diylc.common.ITask; import org.diylc.core.IView; import org.diylc.images.IconLoader; -import org.diylc.plugins.cloud.model.UserEntity; import org.diylc.plugins.cloud.presenter.CloudException; import org.diylc.plugins.cloud.presenter.CloudListener; import org.diylc.plugins.cloud.presenter.CloudPresenter; @@ -25,8 +24,8 @@ import org.diylc.swing.gui.DummyView; import org.diylc.swing.plugins.cloud.view.ChangePasswordDialog; import org.diylc.swing.plugins.cloud.view.CloudBrowserFrame; import org.diylc.swing.plugins.cloud.view.LoginDialog; -import org.diylc.swing.plugins.cloud.view.UserEditDialog; import org.diylc.swing.plugins.cloud.view.UploadDialog; +import org.diylc.swing.plugins.cloud.view.UserEditDialog; import org.diylc.swing.plugins.file.FileFilterEnum; import org.diylc.swingframework.ButtonDialog; import org.diylc.swingframework.ProgressDialog; @@ -53,6 +52,8 @@ public class CloudPlugIn implements IPlugIn, CloudListener { private ChangePasswordAction changePasswordAction; private ManageProjectsAction manageProjectsAction; + private CloudBrowserFrame cloudBrowser; + public CloudPlugIn(ISwingUI swingUI) { super(); @@ -110,6 +111,14 @@ public class CloudPlugIn implements IPlugIn, CloudListener { }); } + public CloudBrowserFrame getCloudBrowser() { + if (cloudBrowser == null) { + cloudBrowser = new CloudBrowserFrame(swingUI.getOwnerFrame(), + plugInPort, cloudPresenter); + } + return cloudBrowser; + } + public LibraryAction getLibraryAction() { if (libraryAction == null) { libraryAction = new LibraryAction(); @@ -186,10 +195,9 @@ public class CloudPlugIn implements IPlugIn, CloudListener { } @Override - public void actionPerformed(ActionEvent e) { - CloudBrowserFrame frame = DialogFactory.getInstance() - .createCloudBrowserFrame(plugInPort, cloudPresenter); - frame.setVisible(true); + public void actionPerformed(ActionEvent e) { + getCloudBrowser().setVisible(true); + getCloudBrowser().requestFocus(); } } diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/ChangePasswordDialog.java b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/ChangePasswordDialog.java index c62064bc..58947acb 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/ChangePasswordDialog.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/ChangePasswordDialog.java @@ -37,15 +37,15 @@ public class ChangePasswordDialog extends ButtonDialog { layoutGui(); refreshState(); } - + public String getNewPassword() { return newPassword; } - + public String getOldPassword() { return oldPassword; } - + @Override protected JComponent getMainComponent() { if (mainPanel == null) { @@ -90,8 +90,7 @@ public class ChangePasswordDialog extends ButtonDialog { this.oldPassword = new String(getOldPasswordField().getPassword()); String newPassword = new String(getNewPasswordField().getPassword()); - String confirmPassword = new String(getConfirmPasswordField() - .getPassword()); + String confirmPassword = new String(getConfirmPasswordField().getPassword()); if (newPassword.equals(confirmPassword)) { this.newPassword = newPassword; } else { @@ -99,33 +98,30 @@ public class ChangePasswordDialog extends ButtonDialog { } JButton okButton = getButton(OK); - okButton.setEnabled((this.newPassword != null) - && (this.newPassword.length() > 0) - && (this.oldPassword != null) + okButton.setEnabled((this.newPassword != null) && (this.newPassword.length() > 0) && (this.oldPassword != null) && (this.oldPassword.length() > 0)); } private JPasswordField getOldPasswordField() { if (oldPasswordField == null) { oldPasswordField = new JPasswordField(); - oldPasswordField.getDocument().addDocumentListener( - new DocumentListener() { + oldPasswordField.getDocument().addDocumentListener(new DocumentListener() { - @Override - public void removeUpdate(DocumentEvent e) { - refreshState(); - } + @Override + public void removeUpdate(DocumentEvent e) { + refreshState(); + } - @Override - public void insertUpdate(DocumentEvent e) { - refreshState(); - } + @Override + public void insertUpdate(DocumentEvent e) { + refreshState(); + } - @Override - public void changedUpdate(DocumentEvent e) { - refreshState(); - } - }); + @Override + public void changedUpdate(DocumentEvent e) { + refreshState(); + } + }); } return oldPasswordField; } @@ -133,24 +129,23 @@ public class ChangePasswordDialog extends ButtonDialog { private JPasswordField getNewPasswordField() { if (newPasswordField == null) { newPasswordField = new JPasswordField(); - newPasswordField.getDocument().addDocumentListener( - new DocumentListener() { + newPasswordField.getDocument().addDocumentListener(new DocumentListener() { - @Override - public void removeUpdate(DocumentEvent e) { - refreshState(); - } + @Override + public void removeUpdate(DocumentEvent e) { + refreshState(); + } - @Override - public void insertUpdate(DocumentEvent e) { - refreshState(); - } + @Override + public void insertUpdate(DocumentEvent e) { + refreshState(); + } - @Override - public void changedUpdate(DocumentEvent e) { - refreshState(); - } - }); + @Override + public void changedUpdate(DocumentEvent e) { + refreshState(); + } + }); } return newPasswordField; } @@ -158,24 +153,23 @@ public class ChangePasswordDialog extends ButtonDialog { private JPasswordField getConfirmPasswordField() { if (confirmPasswordField == null) { confirmPasswordField = new JPasswordField(); - confirmPasswordField.getDocument().addDocumentListener( - new DocumentListener() { + confirmPasswordField.getDocument().addDocumentListener(new DocumentListener() { - @Override - public void removeUpdate(DocumentEvent e) { - refreshState(); - } + @Override + public void removeUpdate(DocumentEvent e) { + refreshState(); + } - @Override - public void insertUpdate(DocumentEvent e) { - refreshState(); - } + @Override + public void insertUpdate(DocumentEvent e) { + refreshState(); + } - @Override - public void changedUpdate(DocumentEvent e) { - refreshState(); - } - }); + @Override + public void changedUpdate(DocumentEvent e) { + refreshState(); + } + }); } return confirmPasswordField; } diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/CloudBrowserFrame.java b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/CloudBrowserFrame.java index 8739e5bb..e60eb551 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/CloudBrowserFrame.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/CloudBrowserFrame.java @@ -9,8 +9,12 @@ import java.awt.Image; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; +import java.nio.channels.Channels; +import java.nio.channels.ReadableByteChannel; import java.util.Iterator; import java.util.List; import java.util.concurrent.ExecutionException; @@ -29,6 +33,8 @@ import javax.swing.JSeparator; import javax.swing.JTabbedPane; import javax.swing.JTextField; import javax.swing.SwingWorker; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; import org.apache.log4j.Logger; import org.diylc.common.IPlugInPort; @@ -38,7 +44,10 @@ import org.diylc.images.IconLoader; import org.diylc.plugins.cloud.model.ProjectEntity; import org.diylc.plugins.cloud.presenter.CloudException; import org.diylc.plugins.cloud.presenter.CloudPresenter; +import org.diylc.swing.ISwingUI; import org.diylc.swing.gui.CustomGlassPane; +import org.diylc.swing.gui.DialogFactory; +import org.diylc.swing.plugins.file.FileFilterEnum; public class CloudBrowserFrame extends JFrame { @@ -66,8 +75,7 @@ public class CloudBrowserFrame extends JFrame { private int pageNumber; private int itemsPerPage = 10; - public CloudBrowserFrame(JFrame owner, IPlugInPort plugInPort, - CloudPresenter cloudPresenter) { + public CloudBrowserFrame(JFrame owner, IPlugInPort plugInPort, CloudPresenter cloudPresenter) { super("DIY Cloud Browser"); this.setIconImage(IconLoader.Cloud.getImage()); this.setPreferredSize(new Dimension(800, 600)); @@ -100,10 +108,16 @@ public class CloudBrowserFrame extends JFrame { private JTabbedPane getTabbedPane() { if (tabbedPane == null) { tabbedPane = new JTabbedPane(); - tabbedPane.addTab("Dashboard", IconLoader.Dashboard.getIcon(), - getDashboardPanel()); - tabbedPane.addTab("Search For Projects", IconLoader.Find.getIcon(), - getBrowsePanel()); + tabbedPane.addTab("Dashboard", IconLoader.Dashboard.getIcon(), getDashboardPanel()); + tabbedPane.addTab("Search For Projects", IconLoader.Find.getIcon(), getBrowsePanel()); + tabbedPane.addChangeListener(new ChangeListener() { + + @Override + public void stateChanged(ChangeEvent e) { + if (getTabbedPane().getSelectedIndex() == 1) + getSearchField().requestFocus(); + } + }); } return tabbedPane; } @@ -119,6 +133,7 @@ public class CloudBrowserFrame extends JFrame { if (browsePanel == null) { browsePanel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); + browsePanel.setBackground(Color.white); gbc.anchor = GridBagConstraints.NORTH; gbc.insets = new Insets(2, 2, 2, 2); gbc.gridx = 0; @@ -127,6 +142,10 @@ public class CloudBrowserFrame extends JFrame { gbc.weightx = 1; browsePanel.add(getSearchPanel(), gbc); + // gbc.gridy = 1; + // gbc.fill = GridBagConstraints.HORIZONTAL; + // browsePanel.add(new JSeparator(), gbc); + gbc.gridy = 1; gbc.weighty = 1; gbc.fill = GridBagConstraints.BOTH; @@ -138,6 +157,7 @@ public class CloudBrowserFrame extends JFrame { private JPanel getSearchPanel() { if (searchPanel == null) { searchPanel = new JPanel(new GridBagLayout()); + searchPanel.setBackground(Color.white); GridBagConstraints gbc = new GridBagConstraints(); gbc.anchor = GridBagConstraints.LINE_START; gbc.insets = new Insets(2, 2, 2, 2); @@ -194,7 +214,12 @@ public class CloudBrowserFrame extends JFrame { private JComboBox getSortBox() { if (sortBox == null) { - sortBox = new JComboBox(); + try { + sortBox = new JComboBox(cloudPresenter.getSortings()); + } catch (CloudException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } return sortBox; } @@ -203,6 +228,7 @@ public class CloudBrowserFrame extends JFrame { if (resultsScrollPane == null) { resultsScrollPane = new JScrollPane(getResultsPanel()); resultsScrollPane.getVerticalScrollBar().setUnitIncrement(16); + resultsScrollPane.setBorder(null); } return resultsScrollPane; } @@ -227,36 +253,26 @@ public class CloudBrowserFrame extends JFrame { executeBackgroundTask(new ITask>() { @Override - public List doInBackground() - throws Exception { - return cloudPresenter.search(getSearchField() - .getText(), getCategoryBox() - .getSelectedItem() == null ? "" - : getCategoryBox().getSelectedItem() - .toString(), - getSortBox().getSelectedItem() == null ? "" - : getSortBox().getSelectedItem() - .toString(), - CloudBrowserFrame.this.pageNumber, - CloudBrowserFrame.this.itemsPerPage); + public List doInBackground() throws Exception { + return cloudPresenter.search(getSearchField().getText(), + getCategoryBox().getSelectedItem() == null ? "" : getCategoryBox() + .getSelectedItem().toString(), getSortBox().getSelectedItem() == null ? "" + : getSortBox().getSelectedItem().toString(), + CloudBrowserFrame.this.pageNumber, CloudBrowserFrame.this.itemsPerPage); } @Override public void failed(Exception e) { - showMessage( - "Search failed. Error: " + e.getMessage(), - "Search Failed", IView.ERROR_MESSAGE); + showMessage("Search failed. Error: " + e.getMessage(), "Search Failed", IView.ERROR_MESSAGE); } @Override public void complete(List result) { getResultsPanel().removeAll(); int count = 0; - for (Iterator i = result.iterator(); i - .hasNext();) { + for (Iterator i = result.iterator(); i.hasNext();) { ProjectEntity project = i.next(); - addProjectToDisplay(project, count++, - i.hasNext()); + addProjectToDisplay(project, count++, i.hasNext()); } } }); @@ -293,27 +309,81 @@ public class CloudBrowserFrame extends JFrame { worker.execute(); } - private void addProjectToDisplay(ProjectEntity project, int location, - boolean hasNext) { - JLabel thumbnailLabel = new JLabel( - downloadImage(project.getThumbnailUrl())); - JLabel nameLabel = new JLabel("" + project.getName() - + ""); + private void addProjectToDisplay(final ProjectEntity project, int location, boolean hasNext) { + JLabel thumbnailLabel = new JLabel(downloadImage(project.getThumbnailUrl())); + JLabel nameLabel = new JLabel("" + project.getName() + ""); nameLabel.setFont(nameLabel.getFont().deriveFont(12f)); - JLabel descriptionLabel = new JLabel("" - + project.getDescription() + ""); + JLabel descriptionLabel = new JLabel("" + project.getDescription() + ""); JLabel categoryLabel = new JLabel("Category: " + project.getCategory()); - JLabel authorLabel = new JLabel("by: " + project.getOwner() - + ""); - JLabel updatedLabel = new JLabel("Last updated: " - + project.getUpdated()); + JLabel authorLabel = new JLabel("by: " + project.getOwner() + ""); + JLabel updatedLabel = new JLabel("Last updated: " + project.getUpdated()); JButton downloadButton = new JButton(IconLoader.CloudDownload.getIcon()); downloadButton.setBorderPainted(false); // downloadButton.setFocusPainted(false); downloadButton.setContentAreaFilled(false); - downloadButton - .setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); + downloadButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); + downloadButton.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + final File file = DialogFactory.getInstance().showSaveDialog(CloudBrowserFrame.this, + FileFilterEnum.DIY.getFilter(), new File(project.getName() + ".diy"), + FileFilterEnum.DIY.getExtensions()[0], null); + if (file != null) { + CloudBrowserFrame.this.executeBackgroundTask(new ITask() { + + @Override + public Void doInBackground() throws Exception { + LOG.debug("Downloading project to to " + file.getAbsolutePath()); + URL website = new URL(project.getDownloadUrl()); + ReadableByteChannel rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(file.getAbsolutePath()); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + return null; + } + + @Override + public void complete(Void result) { + if (CloudBrowserFrame.this.showConfirmDialog( + "Project downloaded to " + file.getAbsolutePath() + ".\nDo you want to open it?", + "Cloud", ISwingUI.YES_NO_OPTION, ISwingUI.INFORMATION_MESSAGE) == IView.YES_OPTION) { + if (!plugInPort.allowFileAction()) { + return; + } + + CloudBrowserFrame.this.executeBackgroundTask(new ITask() { + + @Override + public Void doInBackground() throws Exception { + LOG.debug("Opening from " + file.getAbsolutePath()); + plugInPort.loadProjectFromFile(file.getAbsolutePath()); + return null; + } + + @Override + public void complete(Void result) { + } + + @Override + public void failed(Exception e) { + CloudBrowserFrame.this.showMessage("Could not open file. " + e.getMessage(), + "Error", ISwingUI.ERROR_MESSAGE); + } + }); + } + } + + @Override + public void failed(Exception e) { + CloudBrowserFrame.this.showMessage("Could not save to file. " + e.getMessage(), "Error", + ISwingUI.ERROR_MESSAGE); + } + }); + } + } + }); GridBagConstraints gbc = new GridBagConstraints(); gbc.anchor = GridBagConstraints.NORTHWEST; @@ -328,14 +398,19 @@ public class CloudBrowserFrame extends JFrame { gbc.gridheight = 1; gbc.gridx = 1; gbc.weightx = 1; + gbc.insets = new Insets(2, 6, 2, 2); getResultsPanel().add(nameLabel, gbc); gbc.gridx = 2; + gbc.anchor = GridBagConstraints.NORTHEAST; + gbc.insets = new Insets(2, 2, 2, 8); getResultsPanel().add(authorLabel, gbc); gbc.gridy++; gbc.gridx = 1; gbc.weighty = 1; + gbc.anchor = GridBagConstraints.NORTHWEST; + gbc.insets = new Insets(2, 6, 2, 2); getResultsPanel().add(descriptionLabel, gbc); gbc.gridy++; @@ -345,11 +420,15 @@ public class CloudBrowserFrame extends JFrame { gbc.gridx = 2; gbc.gridheight = 2; + gbc.insets = new Insets(2, 2, 2, 2); + gbc.anchor = GridBagConstraints.SOUTHEAST; getResultsPanel().add(downloadButton, gbc); gbc.gridy++; gbc.gridx = 1; gbc.gridheight = 1; + gbc.insets = new Insets(2, 6, 2, 2); + gbc.anchor = GridBagConstraints.NORTHWEST; getResultsPanel().add(updatedLabel, gbc); if (hasNext) { @@ -358,6 +437,7 @@ public class CloudBrowserFrame extends JFrame { gbc.gridwidth = 3; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.weightx = 1; + gbc.insets = new Insets(2, 2, 2, 2); getResultsPanel().add(new JSeparator(), gbc); } } @@ -366,6 +446,10 @@ public class CloudBrowserFrame extends JFrame { JOptionPane.showMessageDialog(this, message, title, messageType); } + public int showConfirmDialog(String message, String title, int optionType, int messageType) { + return JOptionPane.showConfirmDialog(this, message, title, optionType, messageType); + } + private Icon downloadImage(String imageUrl) { Image image = null; try { diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/UploadDialog.java b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/UploadDialog.java index c69db156..b15d8607 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/UploadDialog.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/UploadDialog.java @@ -60,8 +60,7 @@ public class UploadDialog extends ButtonDialog { private IPlugInPort plugInPort; private CloudPresenter cloudPresenter; - public UploadDialog(JFrame owner, IPlugInPort plugInPort, - CloudPresenter cloudPresenter) { + public UploadDialog(JFrame owner, IPlugInPort plugInPort, CloudPresenter cloudPresenter) { super(owner, "Upload A Project", new String[] { OK, CANCEL }); this.plugInPort = plugInPort; this.cloudPresenter = cloudPresenter; @@ -158,8 +157,7 @@ public class UploadDialog extends ButtonDialog { termsLabel = new JLabel(); termsLabel.setOpaque(true); termsLabel.setBackground(UIManager.getColor("ToolTip.background")); - termsLabel.setBorder(BorderFactory.createCompoundBorder( - BorderFactory.createEtchedBorder(), + termsLabel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(2, 2, 2, 2))); termsLabel.setText(TERMS_HTML); } @@ -189,14 +187,11 @@ public class UploadDialog extends ButtonDialog { paintThumbnail(g, getBounds()); } }, gbc); - Dimension d = UploadDialog.this.plugInPort - .getCanvasDimensions(false); + Dimension d = UploadDialog.this.plugInPort.getCanvasDimensions(false); if (d.height > d.width) - thumbnailPanel.setPreferredSize(new Dimension(192 * d.width - / d.height, 192)); + thumbnailPanel.setPreferredSize(new Dimension(192 * d.width / d.height, 192)); else - thumbnailPanel.setPreferredSize(new Dimension(192, 192 - * d.height / d.width)); + thumbnailPanel.setPreferredSize(new Dimension(192, 192 * d.height / d.width)); thumbnailPanel.setBorder(BorderFactory.createEtchedBorder()); } return thumbnailPanel; @@ -208,10 +203,8 @@ public class UploadDialog extends ButtonDialog { // set default from the project for (int i = 0; i < plugInPort.getProjectProperties().size(); i++) - if (plugInPort.getProjectProperties().get(i).getName() - .equals("Title")) - nameField.setText(plugInPort.getProjectProperties().get(i) - .getValue().toString()); + if (plugInPort.getProjectProperties().get(i).getName().equals("Title")) + nameField.setText(plugInPort.getProjectProperties().get(i).getValue().toString()); } return nameField; } @@ -234,10 +227,8 @@ public class UploadDialog extends ButtonDialog { // set default from the project for (int i = 0; i < plugInPort.getProjectProperties().size(); i++) - if (plugInPort.getProjectProperties().get(i).getName() - .equals("Description")) - descriptionArea.setText(plugInPort.getProjectProperties() - .get(i).getValue().toString()); + if (plugInPort.getProjectProperties().get(i).getName().equals("Description")) + descriptionArea.setText(plugInPort.getProjectProperties().get(i).getValue().toString()); descriptionArea.setBorder(getNameField().getBorder()); descriptionArea.setFont(getNameField().getFont()); } @@ -264,13 +255,11 @@ public class UploadDialog extends ButtonDialog { } public BufferedImage getThumbnail() { - BufferedImage thumbnailImage = new BufferedImage(getThumbnailPanel() - .getWidth(), getThumbnailPanel().getHeight(), - BufferedImage.TYPE_INT_RGB); + BufferedImage thumbnailImage = new BufferedImage(getThumbnailPanel().getWidth(), getThumbnailPanel() + .getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D cg = thumbnailImage.createGraphics(); - paintThumbnail(cg, new Rectangle(thumbnailImage.getWidth(), - thumbnailImage.getHeight())); + paintThumbnail(cg, new Rectangle(thumbnailImage.getWidth(), thumbnailImage.getHeight())); return thumbnailImage; } @@ -291,8 +280,7 @@ public class UploadDialog extends ButtonDialog { } g2d.scale(zoomRatio, zoomRatio); - UploadDialog.this.plugInPort.draw(g2d, - EnumSet.of(DrawOption.ANTIALIASING), null); + UploadDialog.this.plugInPort.draw(g2d, EnumSet.of(DrawOption.ANTIALIASING), null); } public String getKeywords() { diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/UserEditDialog.java b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/UserEditDialog.java index 159574d3..056d8526 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/UserEditDialog.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/cloud/view/UserEditDialog.java @@ -42,8 +42,7 @@ public class UserEditDialog extends ButtonDialog { private UserEntity existingEntity; public UserEditDialog(JFrame owner, UserEntity existingEntity) { - super(owner, existingEntity == null ? "New Account" : "Manage Account", - new String[] { OK, CANCEL }); + super(owner, existingEntity == null ? "New Account" : "Manage Account", new String[] { OK, CANCEL }); this.existingEntity = existingEntity; @@ -136,8 +135,7 @@ public class UserEditDialog extends ButtonDialog { private void refreshState() { this.userName = getUserNameField().getText(); String password = new String(getPasswordField().getPassword()); - String confirmPassword = new String(getConfirmPasswordField() - .getPassword()); + String confirmPassword = new String(getConfirmPasswordField().getPassword()); if (password.equals(confirmPassword)) { this.password = password; } else { @@ -148,8 +146,7 @@ public class UserEditDialog extends ButtonDialog { this.bio = getBioArea().getText(); JButton okButton = getButton(OK); okButton.setEnabled((this.userName.length() > 0) - && (this.existingEntity != null || ((this.password != null) && (this.password - .length() > 0)))); + && (this.existingEntity != null || ((this.password != null) && (this.password.length() > 0)))); } private JTextField getUserNameField() { @@ -159,24 +156,23 @@ public class UserEditDialog extends ButtonDialog { userNameField.setText(existingEntity.getUsername()); userNameField.setEditable(false); } - userNameField.getDocument().addDocumentListener( - new DocumentListener() { + userNameField.getDocument().addDocumentListener(new DocumentListener() { - @Override - public void removeUpdate(DocumentEvent e) { - refreshState(); - } + @Override + public void removeUpdate(DocumentEvent e) { + refreshState(); + } - @Override - public void insertUpdate(DocumentEvent e) { - refreshState(); - } + @Override + public void insertUpdate(DocumentEvent e) { + refreshState(); + } - @Override - public void changedUpdate(DocumentEvent e) { - refreshState(); - } - }); + @Override + public void changedUpdate(DocumentEvent e) { + refreshState(); + } + }); } return userNameField; } @@ -184,24 +180,23 @@ public class UserEditDialog extends ButtonDialog { private JPasswordField getPasswordField() { if (passwordField == null) { passwordField = new JPasswordField(); - passwordField.getDocument().addDocumentListener( - new DocumentListener() { + passwordField.getDocument().addDocumentListener(new DocumentListener() { - @Override - public void removeUpdate(DocumentEvent e) { - refreshState(); - } + @Override + public void removeUpdate(DocumentEvent e) { + refreshState(); + } - @Override - public void insertUpdate(DocumentEvent e) { - refreshState(); - } + @Override + public void insertUpdate(DocumentEvent e) { + refreshState(); + } - @Override - public void changedUpdate(DocumentEvent e) { - refreshState(); - } - }); + @Override + public void changedUpdate(DocumentEvent e) { + refreshState(); + } + }); } return passwordField; } @@ -209,24 +204,23 @@ public class UserEditDialog extends ButtonDialog { private JPasswordField getConfirmPasswordField() { if (confirmPasswordField == null) { confirmPasswordField = new JPasswordField(); - confirmPasswordField.getDocument().addDocumentListener( - new DocumentListener() { + confirmPasswordField.getDocument().addDocumentListener(new DocumentListener() { - @Override - public void removeUpdate(DocumentEvent e) { - refreshState(); - } + @Override + public void removeUpdate(DocumentEvent e) { + refreshState(); + } - @Override - public void insertUpdate(DocumentEvent e) { - refreshState(); - } + @Override + public void insertUpdate(DocumentEvent e) { + refreshState(); + } - @Override - public void changedUpdate(DocumentEvent e) { - refreshState(); - } - }); + @Override + public void changedUpdate(DocumentEvent e) { + refreshState(); + } + }); } return confirmPasswordField; } @@ -236,24 +230,23 @@ public class UserEditDialog extends ButtonDialog { emailField = new JTextField(); if (existingEntity != null) emailField.setText(existingEntity.getEmail()); - emailField.getDocument().addDocumentListener( - new DocumentListener() { + emailField.getDocument().addDocumentListener(new DocumentListener() { - @Override - public void removeUpdate(DocumentEvent e) { - refreshState(); - } + @Override + public void removeUpdate(DocumentEvent e) { + refreshState(); + } - @Override - public void insertUpdate(DocumentEvent e) { - refreshState(); - } + @Override + public void insertUpdate(DocumentEvent e) { + refreshState(); + } - @Override - public void changedUpdate(DocumentEvent e) { - refreshState(); - } - }); + @Override + public void changedUpdate(DocumentEvent e) { + refreshState(); + } + }); } return emailField; } @@ -263,24 +256,23 @@ public class UserEditDialog extends ButtonDialog { websiteField = new JTextField(); if (existingEntity != null) websiteField.setText(existingEntity.getWebsite()); - websiteField.getDocument().addDocumentListener( - new DocumentListener() { + websiteField.getDocument().addDocumentListener(new DocumentListener() { - @Override - public void removeUpdate(DocumentEvent e) { - refreshState(); - } + @Override + public void removeUpdate(DocumentEvent e) { + refreshState(); + } - @Override - public void insertUpdate(DocumentEvent e) { - refreshState(); - } + @Override + public void insertUpdate(DocumentEvent e) { + refreshState(); + } - @Override - public void changedUpdate(DocumentEvent e) { - refreshState(); - } - }); + @Override + public void changedUpdate(DocumentEvent e) { + refreshState(); + } + }); } return websiteField; } diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/file/BomDialog.java b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/file/BomDialog.java index 1d8c4921..373942c3 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/file/BomDialog.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/src/org/diylc/swing/plugins/file/BomDialog.java @@ -55,8 +55,8 @@ public class BomDialog extends JDialog { private ObjectListTable getTable() { if (table == null) { try { - table = new ObjectListTable(BomEntry.class, new String[] { "getName", - "getValue", "getQuantity", "getNotes/setNotes" }, null); + table = new ObjectListTable(BomEntry.class, new String[] { "getName", "getValue", + "getQuantity", "getNotes/setNotes" }, null); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { @@ -89,9 +89,8 @@ public class BomDialog extends JDialog { @Override public void actionPerformed(ActionEvent e) { - File file = DialogFactory.getInstance().showSaveDialog( - FileFilterEnum.EXCEL.getFilter(), null, - FileFilterEnum.EXCEL.getExtensions()[0], null); + File file = DialogFactory.getInstance().showSaveDialog(BomDialog.this.getOwner(), + FileFilterEnum.EXCEL.getFilter(), null, FileFilterEnum.EXCEL.getExtensions()[0], null); if (file != null) { try { TableExporter.getInstance().exportToExcel(getTable(), file); @@ -115,8 +114,8 @@ public class BomDialog extends JDialog { @Override public void actionPerformed(ActionEvent e) { - File file = DialogFactory.getInstance().showSaveDialog(FileFilterEnum.CSV.getFilter(), - null, FileFilterEnum.CSV.getExtensions()[0], null); + File file = DialogFactory.getInstance().showSaveDialog(BomDialog.this.getOwner(), + FileFilterEnum.CSV.getFilter(), null, FileFilterEnum.CSV.getExtensions()[0], null); if (file != null) { try { TableExporter.getInstance().exportToCSV(getTable(), file); @@ -140,8 +139,8 @@ public class BomDialog extends JDialog { @Override public void actionPerformed(ActionEvent e) { - File file = DialogFactory.getInstance().showSaveDialog(FileFilterEnum.HTML.getFilter(), - null, FileFilterEnum.HTML.getExtensions()[0], null); + File file = DialogFactory.getInstance().showSaveDialog(BomDialog.this.getOwner(), + FileFilterEnum.HTML.getFilter(), null, FileFilterEnum.HTML.getExtensions()[0], null); if (file != null) { try { TableExporter.getInstance().exportToHTML(getTable(), file); @@ -165,8 +164,8 @@ public class BomDialog extends JDialog { @Override public void actionPerformed(ActionEvent e) { - File file = DialogFactory.getInstance().showSaveDialog(FileFilterEnum.PNG.getFilter(), - null, FileFilterEnum.PNG.getExtensions()[0], null); + File file = DialogFactory.getInstance().showSaveDialog(BomDialog.this.getOwner(), + FileFilterEnum.PNG.getFilter(), null, FileFilterEnum.PNG.getExtensions()[0], null); if (file != null) { try { TableExporter.getInstance().exportToPNG(getTable(), file);