code cleanup

V1 file import - added support for more component types
minor improvements
This commit is contained in:
Branislav Stojkovic
2016-11-02 01:04:52 +01:00
parent 94c8512c11
commit ad6e45da23
24 changed files with 285 additions and 139 deletions
@@ -12,6 +12,11 @@ import org.diylc.common.PropertyWrapper;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.AbstractMeasure;
/**
* Based on {@link PropertyWrapper#getType()}, creates an appropriate {@link Component} that can edit that type.
*
* @author Branislav Stojkovic
*/
public class FieldEditorFactory {
private static final Logger LOG = Logger.getLogger(FieldEditorFactory.class);
@@ -5,6 +5,8 @@ import java.awt.Dimension;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
@@ -22,11 +24,14 @@ public class MultiLineStringEditor extends JScrollPane {
private final PropertyWrapper property;
private static final Border border = new JTextField().getBorder();
public MultiLineStringEditor(PropertyWrapper property) {
super();
this.property = property;
setViewportView(getTextArea());
setPreferredSize(new Dimension(192, 64));
setBorder(border);
}
public JTextArea getTextArea() {
@@ -52,21 +52,6 @@ public class PropertyEditorDialog extends ButtonDialog {
layoutGui();
setLocationRelativeTo(owner);
//
// setModal(true);
//
// JPanel holderPanel = new JPanel();
// holderPanel.setLayout(new BorderLayout());
// holderPanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
//
// holderPanel.add(createEditorFields(), BorderLayout.CENTER);
// holderPanel.add(createButtonPanel(), BorderLayout.SOUTH);
//
// setContentPane(holderPanel);
//
//
// pack();
// setLocationRelativeTo(owner);
}
@Override
@@ -163,15 +148,6 @@ public class PropertyEditorDialog extends ButtonDialog {
return defaultedProperties;
}
public static boolean showFor(JFrame owner, List<PropertyWrapper> properties, String title) {
PropertyEditorDialog editor = new PropertyEditorDialog(owner, properties, title);
editor.setVisible(true);
if (OK.equals(editor.getSelectedButtonCaption())) {
return true;
}
return false;
}
@Override
public void setVisible(boolean b) {
if (b && componentToFocus != null) {
@@ -15,7 +15,6 @@ import org.diylc.common.ITask;
import org.diylc.core.IView;
import org.diylc.images.IconLoader;
import org.diylc.plugins.cloud.presenter.CloudException;
import org.diylc.plugins.cloud.presenter.CloudListener;
import org.diylc.plugins.cloud.presenter.CloudPresenter;
import org.diylc.presenter.Presenter;
import org.diylc.swing.ISwingUI;
@@ -30,7 +29,7 @@ import org.diylc.swing.plugins.cloud.view.browser.UploadManagerFrame;
import org.diylc.swing.plugins.file.FileFilterEnum;
import org.diylc.swingframework.ButtonDialog;
public class CloudPlugIn implements IPlugIn, CloudListener {
public class CloudPlugIn implements IPlugIn {
private static final String ONLINE_TITLE = "Project Cloud";
@@ -39,7 +38,6 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
private ISwingUI swingUI;
private IPlugInPort plugInPort;
private IPlugInPort thumbnailPresenter;
private CloudPresenter cloudPresenter;
private LibraryAction libraryAction;
@@ -81,7 +79,6 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
@Override
public void connect(IPlugInPort plugInPort) {
this.plugInPort = plugInPort;
this.cloudPresenter = new CloudPresenter(this);
initialize();
}
@@ -91,7 +88,7 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
@Override
public Boolean doInBackground() throws Exception {
return cloudPresenter.tryLogInWithToken();
return CloudPresenter.Instance.tryLogInWithToken();
}
@Override
@@ -113,13 +110,13 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
public CloudBrowserFrame getCloudBrowser() {
if (cloudBrowser == null) {
cloudBrowser = new CloudBrowserFrame(swingUI, plugInPort, cloudPresenter);
cloudBrowser = new CloudBrowserFrame(swingUI, plugInPort);
}
return cloudBrowser;
}
public UploadManagerFrame createUploadManagerFrame() {
return new UploadManagerFrame(swingUI, plugInPort, cloudPresenter);
return new UploadManagerFrame(swingUI, plugInPort);
}
public LibraryAction getLibraryAction() {
@@ -220,11 +217,12 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
dialog.setVisible(true);
if (ButtonDialog.OK.equals(dialog.getSelectedButtonCaption())) {
try {
if (cloudPresenter.logIn(dialog.getUserName(), dialog.getPassword())) {
if (CloudPresenter.Instance.logIn(dialog.getUserName(), dialog.getPassword())) {
swingUI
.showMessage(
"You have successfully logged into the system. You will remain logged in from this machine until logged out.",
"Login Successful", IView.INFORMATION_MESSAGE);
loggedIn();
break;
} else {
swingUI.showMessage(
@@ -252,7 +250,8 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
@Override
public void actionPerformed(ActionEvent e) {
cloudPresenter.logOut();
CloudPresenter.Instance.logOut();
loggedOut();
}
}
@@ -275,7 +274,7 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
@Override
public Void doInBackground() throws Exception {
cloudPresenter.createUserAccount(dialog.getUserName(), dialog.getPassword(), dialog.getEmail(),
CloudPresenter.Instance.createUserAccount(dialog.getUserName(), dialog.getPassword(), dialog.getEmail(),
dialog.getWebsite(), dialog.getBio());
return null;
}
@@ -308,14 +307,14 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
final UserEditDialog dialog = DialogFactory.getInstance().createUserEditDialog(cloudPresenter.getUserDetails());
final UserEditDialog dialog = DialogFactory.getInstance().createUserEditDialog(CloudPresenter.Instance.getUserDetails());
dialog.setVisible(true);
if (ButtonDialog.OK.equals(dialog.getSelectedButtonCaption())) {
swingUI.executeBackgroundTask(new ITask<Void>() {
@Override
public Void doInBackground() throws Exception {
cloudPresenter.updateUserDetails(dialog.getEmail(), dialog.getWebsite(), dialog.getBio());
CloudPresenter.Instance.updateUserDetails(dialog.getEmail(), dialog.getWebsite(), dialog.getBio());
return null;
}
@@ -357,7 +356,7 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
@Override
public Void doInBackground() throws Exception {
cloudPresenter.updatePassword(dialog.getOldPassword(), dialog.getNewPassword());
CloudPresenter.Instance.updatePassword(dialog.getOldPassword(), dialog.getNewPassword());
return null;
}
@@ -400,7 +399,7 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
public String[] doInBackground() throws Exception {
LOG.debug("Uploading from " + file.getAbsolutePath());
thumbnailPresenter.loadProjectFromFile(file.getAbsolutePath());
return cloudPresenter.getCategories();
return CloudPresenter.Instance.getCategories();
}
@Override
@@ -417,7 +416,7 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
@Override
public Void doInBackground() throws Exception {
cloudPresenter.uploadProject(dialog.getName(), dialog.getCategory(), dialog.getDescription(),
CloudPresenter.Instance.uploadProject(dialog.getName(), dialog.getCategory(), dialog.getDescription(),
dialog.getKeywords(), plugInPort.getCurrentVersionNumber().toString(), thumbnailFile, file,
null);
return null;
@@ -471,7 +470,6 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
}
}
@Override
public void loggedIn() {
getLoginAction().setEnabled(false);
getCreateAccountAction().setEnabled(false);
@@ -482,7 +480,6 @@ public class CloudPlugIn implements IPlugIn, CloudListener {
getManageProjectsAction().setEnabled(true);
}
@Override
public void loggedOut() {
getLoginAction().setEnabled(true);
getCreateAccountAction().setEnabled(true);
@@ -42,7 +42,6 @@ public class CommentDialog extends JDialog {
private ProjectEntity project;
private List<CommentEntity> comments;
private CloudPresenter presenter;
private JPanel mainPanel;
private JPanel listPanel;
@@ -53,19 +52,17 @@ public class CommentDialog extends JDialog {
private ISimpleView cloudUI;
public CommentDialog(ISimpleView cloudUI, ProjectEntity project, List<CommentEntity> comments,
CloudPresenter presenter) {
public CommentDialog(ISimpleView cloudUI, ProjectEntity project, List<CommentEntity> comments) {
super(cloudUI.getOwnerFrame(), "Comments on " + project.getName());
this.project = project;
this.comments = comments;
this.presenter = presenter;
this.cloudUI = cloudUI;
setMinimumSize(new Dimension(400, 600));
setContentPane(getMainPanel());
pack();
setLocationRelativeTo(cloudUI.getOwnerFrame());
setModal(true);
if (!presenter.isLoggedIn()) {
if (!CloudPresenter.Instance.isLoggedIn()) {
getReplyArea().setEnabled(false);
getReplyArea().setText("Must be logged in to reply.");
getSendButton().setEnabled(false);
@@ -148,7 +145,7 @@ public class CommentDialog extends JDialog {
@Override
public Void doInBackground() throws Exception {
presenter.postComment(project.getId(), comment);
CloudPresenter.Instance.postComment(project.getId(), comment);
return null;
}
@@ -162,7 +159,7 @@ public class CommentDialog extends JDialog {
getReplyArea().setText("");
CommentEntity newComment = new CommentEntity();
newComment.setPostedAt(JUST_NOW);
newComment.setUsername(presenter.getCurrentUsername());
newComment.setUsername(CloudPresenter.Instance.getCurrentUsername());
newComment.setComment(comment);
addComment(newComment).requestFocusInWindow();
getListPanel().revalidate();
@@ -34,6 +34,7 @@ public class UserEditDialog extends ButtonDialog {
private JTextField emailField;
private JTextField websiteField;
private JTextArea bioArea;
private JScrollPane bioPane;
private String userName;
private String password;
@@ -130,7 +131,7 @@ public class UserEditDialog extends ButtonDialog {
gbc.gridy = 5;
gbc.fill = GridBagConstraints.BOTH;
mainPanel.add(new JScrollPane(getBioArea()), gbc);
mainPanel.add(getBioPane(), gbc);
}
return mainPanel;
}
@@ -254,7 +255,7 @@ public class UserEditDialog extends ButtonDialog {
return emailField;
}
public JTextField getWebsiteField() {
private JTextField getWebsiteField() {
if (websiteField == null) {
websiteField = new JTextField();
if (existingEntity != null)
@@ -280,14 +281,23 @@ public class UserEditDialog extends ButtonDialog {
return websiteField;
}
public JTextArea getBioArea() {
private JScrollPane getBioPane() {
if (bioPane == null) {
bioPane = new JScrollPane(getBioArea());
bioPane.setPreferredSize(new Dimension(192, 64));
bioPane.setBorder(getBioArea().getBorder());
getBioArea().setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
}
return bioPane;
}
private JTextArea getBioArea() {
if (bioArea == null) {
bioArea = new HTMLTextArea();
if (existingEntity != null)
bioArea.setText(existingEntity.getBio());
bioArea.setFont(getUserNameField().getFont());
bioArea.setBorder(getUserNameField().getBorder());
bioArea.setPreferredSize(new Dimension(192, 69));
bioArea.getDocument().addDocumentListener(new DocumentListener() {
@Override
@@ -27,7 +27,7 @@ import org.diylc.core.IView;
import org.diylc.images.IconLoader;
import org.diylc.plugins.cloud.model.ProjectEntity;
import org.diylc.plugins.cloud.presenter.CloudPresenter;
import org.diylc.plugins.cloud.presenter.PagingProvider;
import org.diylc.plugins.cloud.presenter.SearchSession;
import org.diylc.swing.ISimpleView;
import org.diylc.swing.ISwingUI;
import org.diylc.utils.Pair;
@@ -54,19 +54,17 @@ public class CloudBrowserFrame extends JFrame implements ISimpleView {
private ResultsScrollPanel resultsScrollPane;
private IPlugInPort plugInPort;
private CloudPresenter cloudPresenter;
private PagingProvider pagingProvider;
private SearchSession searchSession;
private ISwingUI swingUI;
public CloudBrowserFrame(ISwingUI swingUI, IPlugInPort plugInPort, CloudPresenter cloudPresenter) {
public CloudBrowserFrame(ISwingUI swingUI, IPlugInPort plugInPort) {
super(TITLE);
this.swingUI = swingUI;
this.setIconImage(IconLoader.Cloud.getImage());
this.setPreferredSize(new Dimension(700, 640));
this.plugInPort = plugInPort;
this.cloudPresenter = cloudPresenter;
this.pagingProvider = new PagingProvider(cloudPresenter);
this.searchSession = new SearchSession();
setContentPane(getSearchPanel());
this.pack();
@@ -85,7 +83,7 @@ public class CloudBrowserFrame extends JFrame implements ISimpleView {
@Override
public Pair<String[], String[]> doInBackground() throws Exception {
return new Pair<String[], String[]>(cloudPresenter.getCategories(), cloudPresenter.getSortings());
return new Pair<String[], String[]>(CloudPresenter.Instance.getCategories(), CloudPresenter.Instance.getSortings());
}
@Override
@@ -181,7 +179,7 @@ public class CloudBrowserFrame extends JFrame implements ISimpleView {
private ResultsScrollPanel getResultsScrollPane() {
if (resultsScrollPane == null) {
resultsScrollPane = new ResultsScrollPanel(swingUI, this, plugInPort, pagingProvider, cloudPresenter, false);
resultsScrollPane = new ResultsScrollPanel(swingUI, this, plugInPort, searchSession, false);
}
return resultsScrollPane;
}
@@ -192,7 +190,7 @@ public class CloudBrowserFrame extends JFrame implements ISimpleView {
@Override
public List<ProjectEntity> doInBackground() throws Exception {
return pagingProvider.startSession(getSearchHeaderPanel().getSearchText(),
return searchSession.startSession(getSearchHeaderPanel().getSearchText(),
getSearchHeaderPanel().getCategory(), getSearchHeaderPanel().getSorting());
}
@@ -42,7 +42,7 @@ import org.diylc.images.IconLoader;
import org.diylc.plugins.cloud.model.CommentEntity;
import org.diylc.plugins.cloud.model.ProjectEntity;
import org.diylc.plugins.cloud.presenter.CloudPresenter;
import org.diylc.plugins.cloud.presenter.PagingProvider;
import org.diylc.plugins.cloud.presenter.SearchSession;
import org.diylc.presenter.Presenter;
import org.diylc.swing.ISimpleView;
import org.diylc.swing.ISwingUI;
@@ -59,7 +59,7 @@ import org.diylc.swingframework.ButtonDialog;
* Component that is capable of showing a list of {@link ProjectEntity} objects. Can work in paging
* mode and pull one page of data at a time.
*
* @see {@link PagingProvider}
* @see {@link SearchSession}
*
* @author Branislav Stojkovic
*/
@@ -88,22 +88,19 @@ public class ResultsScrollPanel extends JScrollPane {
* This flag tells us whether the loadMoreLabel should invoke a new data pull or not.
*/
private boolean armed;
private PagingProvider provider;
private CloudPresenter cloudPresenter;
private SearchSession searchSession;
private Icon spinnerIcon = IconLoader.Spinning.getIcon();
private boolean showEditControls;
public ResultsScrollPanel(ISwingUI mainUI, ISimpleView cloudUI, IPlugInPort plugInPort, PagingProvider provider,
CloudPresenter cloudPresenter, boolean showEditControls) {
public ResultsScrollPanel(ISwingUI mainUI, ISimpleView cloudUI, IPlugInPort plugInPort, SearchSession searchSession,
boolean showEditControls) {
super();
this.mainUI = mainUI;
this.cloudUI = cloudUI;
this.plugInPort = plugInPort;
this.provider = provider;
this.cloudPresenter = cloudPresenter;
this.searchSession = searchSession;
this.showEditControls = showEditControls;
this.getVerticalScrollBar().setUnitIncrement(16);
@@ -174,7 +171,7 @@ public class ResultsScrollPanel extends JScrollPane {
getViewport().setViewPosition(old);
}
});
if (provider != null && provider.hasMoreData()) {
if (searchSession != null && searchSession.hasMoreData()) {
getLoadMoreLabel().setText("Querying the cloud for more results...");
getLoadMoreLabel().setIcon(spinnerIcon);
} else {
@@ -211,7 +208,7 @@ public class ResultsScrollPanel extends JScrollPane {
@Override
public List<CommentEntity> doInBackground() throws Exception {
return cloudPresenter.getComments(project.getId());
return CloudPresenter.Instance.getComments(project.getId());
}
@Override
@@ -222,7 +219,7 @@ public class ResultsScrollPanel extends JScrollPane {
@Override
public void complete(List<CommentEntity> result) {
CommentDialog dialog = new CommentDialog(cloudUI, project, result, cloudPresenter);
CommentDialog dialog = new CommentDialog(cloudUI, project, result);
dialog.setVisible(true);
}
@@ -320,7 +317,7 @@ public class ResultsScrollPanel extends JScrollPane {
editButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
List<PropertyWrapper> projectProperties = cloudPresenter.getProjectProperties(project);
List<PropertyWrapper> projectProperties = CloudPresenter.Instance.getProjectProperties(project);
PropertyEditorDialog editor =
DialogFactory.getInstance().createPropertyEditorDialog(cloudUI.getOwnerFrame(), projectProperties,
"Edit Published Project");
@@ -341,8 +338,8 @@ public class ResultsScrollPanel extends JScrollPane {
@Override
public ProjectEntity doInBackground() throws Exception {
cloudPresenter.updateProjectDetails(project, plugInPort.getCurrentVersionNumber().toString());
return cloudPresenter.fetchUserUploads(project.getId()).get(0);
CloudPresenter.Instance.updateProjectDetails(project, plugInPort.getCurrentVersionNumber().toString());
return CloudPresenter.Instance.fetchUserUploads(project.getId()).get(0);
}
@Override
@@ -389,7 +386,7 @@ public class ResultsScrollPanel extends JScrollPane {
public String[] doInBackground() throws Exception {
LOG.debug("Uploading from " + file.getAbsolutePath());
thumbnailPresenter.loadProjectFromFile(file.getAbsolutePath());
return cloudPresenter.getCategories();
return CloudPresenter.Instance.getCategories();
}
@Override
@@ -406,10 +403,10 @@ public class ResultsScrollPanel extends JScrollPane {
@Override
public ProjectEntity doInBackground() throws Exception {
cloudPresenter.uploadProject(dialog.getName(), dialog.getCategory(), dialog.getDescription(),
CloudPresenter.Instance.uploadProject(dialog.getName(), dialog.getCategory(), dialog.getDescription(),
dialog.getKeywords(), plugInPort.getCurrentVersionNumber().toString(), thumbnailFile,
file, project.getId());
return cloudPresenter.fetchUserUploads(project.getId()).get(0);
return CloudPresenter.Instance.fetchUserUploads(project.getId()).get(0);
}
@Override
@@ -463,7 +460,7 @@ public class ResultsScrollPanel extends JScrollPane {
@Override
public Void doInBackground() throws Exception {
cloudPresenter.deleteProject(project.getId());
CloudPresenter.Instance.deleteProject(project.getId());
return null;
}
@@ -601,14 +598,14 @@ public class ResultsScrollPanel extends JScrollPane {
@Override
public void paint(Graphics g) {
super.paint(g);
if (armed && provider != null && provider.hasMoreData()) {
if (armed && searchSession != null && searchSession.hasMoreData()) {
// disarm immediately so we don't trigger successive requests to the provider
armed = false;
SwingWorker<List<ProjectEntity>, Void> worker = new SwingWorker<List<ProjectEntity>, Void>() {
@Override
protected List<ProjectEntity> doInBackground() throws Exception {
return provider.requestMoreData();
return searchSession.requestMoreData();
}
@Override
@@ -29,17 +29,15 @@ public class UploadManagerFrame extends JFrame implements ISimpleView {
private ResultsScrollPanel resultsScrollPane;
private IPlugInPort plugInPort;
private CloudPresenter cloudPresenter;
private ISwingUI swingUI;
public UploadManagerFrame(ISwingUI swingUI, IPlugInPort plugInPort, CloudPresenter cloudPresenter) {
public UploadManagerFrame(ISwingUI swingUI, IPlugInPort plugInPort) {
super(TITLE);
this.swingUI = swingUI;
this.setIconImage(IconLoader.CloudGear.getImage());
this.setPreferredSize(new Dimension(700, 640));
this.plugInPort = plugInPort;
this.cloudPresenter = cloudPresenter;
setContentPane(getResultsScrollPane());
this.pack();
@@ -51,7 +49,7 @@ public class UploadManagerFrame extends JFrame implements ISimpleView {
private ResultsScrollPanel getResultsScrollPane() {
if (resultsScrollPane == null) {
resultsScrollPane = new ResultsScrollPanel(swingUI, this, plugInPort, null, cloudPresenter, true);
resultsScrollPane = new ResultsScrollPanel(swingUI, this, plugInPort, null, true);
}
return resultsScrollPane;
}
@@ -62,7 +60,7 @@ public class UploadManagerFrame extends JFrame implements ISimpleView {
@Override
public List<ProjectEntity> doInBackground() throws Exception {
return cloudPresenter.fetchUserUploads(null);
return CloudPresenter.Instance.fetchUserUploads(null);
}
@Override