add announcements

This commit is contained in:
bancika
2016-10-26 18:04:52 +02:00
parent d510483f31
commit 8c85e7351c
8 changed files with 249 additions and 9 deletions
@@ -0,0 +1,73 @@
package org.diylc.announcements;
import java.io.Serializable;
public class Announcement implements Serializable {
private static final long serialVersionUID = 1L;
private String title;
private String text;
private String date;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((date == null) ? 0 : date.hashCode());
result = prime * result + ((text == null) ? 0 : text.hashCode());
result = prime * result + ((title == null) ? 0 : title.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Announcement other = (Announcement) obj;
if (date == null) {
if (other.date != null)
return false;
} else if (!date.equals(other.date))
return false;
if (text == null) {
if (other.text != null)
return false;
} else if (!text.equals(other.text))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
}
@@ -0,0 +1,64 @@
package org.diylc.announcements;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.plugins.cloud.model.IServiceAPI;
import com.diyfever.httpproxy.PhpFlatProxy;
import com.diyfever.httpproxy.ProxyFactory;
public class AnnouncementProvider {
private String serviceUrl = "http://www.diy-fever.com/diylc/api/v1/announcements.html";
private String LAST_READ_KEY = "announcement.lastReadDate";
private IAnnouncementService service;
private Date lastDate;
private List<Announcement> announcements;
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
public AnnouncementProvider() {
String lastDateStr = ConfigurationManager.getInstance().readString(LAST_READ_KEY, null);
try {
this.lastDate = lastDateStr == null ? null : dateFormat.parse(lastDateStr);
} catch (ParseException e) {
}
serviceUrl =
ConfigurationManager.getInstance().readString(IServiceAPI.URL_KEY, "http://www.diy-fever.com/diylc/api/v1");
ProxyFactory factory = new ProxyFactory(new PhpFlatProxy());
service = factory.createProxy(IAnnouncementService.class, serviceUrl);
}
public String getCurrentAnnouncements() throws ParseException {
announcements = service.getAnnouncements();
boolean hasUnread = false;
StringBuilder sb = new StringBuilder("<html>");
for (int i = 0; i < announcements.size(); i++) {
Date date = dateFormat.parse(announcements.get(i).getDate());
if (lastDate == null || lastDate.before(date)) {
sb.append("<font size='4'><b>").append(announcements.get(i).getTitle()).append("</b> on ")
.append(announcements.get(i).getDate()).append("</font>").append("<p>")
.append(announcements.get(i).getText()).append("</p>");
hasUnread = true;
}
}
if (!hasUnread)
return "";
sb.append("<br><font size='small'>Click OK to dismiss and mark these announcements as read.</font>");
sb.append("</html>");
return sb.toString();
}
public void dismissed() {
Date date = new Date();
ConfigurationManager.getInstance().writeValue(LAST_READ_KEY, dateFormat.format(date));
}
}
@@ -0,0 +1,8 @@
package org.diylc.announcements;
import java.util.List;
public interface IAnnouncementService {
public List<Announcement> getAnnouncements();
}
@@ -224,7 +224,7 @@ public class CloudPresenter {
if (res instanceof List<?>) { if (res instanceof List<?>) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<ProjectEntity> projects = (List<ProjectEntity>) res; List<ProjectEntity> projects = (List<ProjectEntity>) res;
LOG.info("Received " + projects.size() + " results"); LOG.info("Received " + projects.size() + " results. Downloading thumbnails...");
// Download thumbnails and replace urls with local paths to speed up loading in the main thread // Download thumbnails and replace urls with local paths to speed up loading in the main thread
for (int i = 0; i < projects.size(); i++) { for (int i = 0; i < projects.size(); i++) {
String url = projects.get(i).getThumbnailUrl(); String url = projects.get(i).getThumbnailUrl();
@@ -236,6 +236,7 @@ public class CloudPresenter {
projects.get(i).setThumbnailUrl(temp.getAbsolutePath()); projects.get(i).setThumbnailUrl(temp.getAbsolutePath());
fos.close(); fos.close();
} }
LOG.info("Finished downloading thumbnails.");
return projects; return projects;
} }
throw new CloudException("Unexpected server response received for search results: " + res.getClass().getName()); throw new CloudException("Unexpected server response received for search results: " + res.getClass().getName());
@@ -34,7 +34,7 @@ public enum IconLoader {
"id_card_edit.png"), KeyEdit("key_edit.png"), Find("find.png"), Dashboard("dashboard.png"), DataFind( "id_card_edit.png"), KeyEdit("key_edit.png"), Find("find.png"), Dashboard("dashboard.png"), DataFind(
"data_find.png"), CloudDownload("cloud-download.png"), NavLeftBlue("nav_left_blue.png"), NavRightBlue( "data_find.png"), CloudDownload("cloud-download.png"), NavLeftBlue("nav_left_blue.png"), NavRightBlue(
"nav_right_blue.png"), MissingImage("missing_image.png"), CloudBg("cloud_bg.png"), CloudBig("cloud_big.png"), CloudWait( "nav_right_blue.png"), MissingImage("missing_image.png"), CloudBg("cloud_bg.png"), CloudBig("cloud_big.png"), CloudWait(
"cloud_wait.png"), Spinning("spinning.gif"); "cloud_wait.png"), Spinning("spinning.gif"), Megaphone("megaphone.png");
protected String name; protected String name;
Binary file not shown.

After

Width:  |  Height:  |  Size: 854 B

@@ -6,6 +6,8 @@ import java.awt.Graphics;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import java.awt.Insets; import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
@@ -28,7 +30,9 @@ import javax.swing.JScrollPane;
import javax.swing.JSeparator; import javax.swing.JSeparator;
import javax.swing.JTextArea; import javax.swing.JTextArea;
import javax.swing.SwingConstants; import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker; import javax.swing.SwingWorker;
import javax.swing.text.DefaultCaret;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.diylc.common.IPlugInPort; import org.diylc.common.IPlugInPort;
@@ -139,11 +143,21 @@ public class ResultsScrollPanel extends JScrollPane {
JLabel thumbnailLabel = new JLabel(loadImage(project.getThumbnailUrl())); JLabel thumbnailLabel = new JLabel(loadImage(project.getThumbnailUrl()));
JLabel nameLabel = new JLabel("<html><b>" + project.getName() + "</b></html>"); JLabel nameLabel = new JLabel("<html><b>" + project.getName() + "</b></html>");
nameLabel.setFont(nameLabel.getFont().deriveFont(12f)); nameLabel.setFont(nameLabel.getFont().deriveFont(12f));
// JLabel descriptionArea = new JLabel("<html><p align='justify'>" + project.getDescription() +
// "</p></html>");
JTextArea descriptionArea = new JTextArea(project.getDescription().replace("<br>", "\n")); JTextArea descriptionArea = new JTextArea(project.getDescription().replace("<br>", "\n"));
descriptionArea.setEditable(false); descriptionArea.setEditable(false);
descriptionArea.setFont(thumbnailLabel.getFont()); descriptionArea.setFont(thumbnailLabel.getFont());
descriptionArea.setLineWrap(true); descriptionArea.setLineWrap(true);
descriptionArea.setWrapStyleWord(true); descriptionArea.setWrapStyleWord(true);
descriptionArea.setFocusable(false);
descriptionArea.setRequestFocusEnabled(false);
DefaultCaret caret = (DefaultCaret) descriptionArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
descriptionArea.setEnabled(false);
// descriptionArea.setDisabledTextColor(descriptionArea.getSelectionColor());
JLabel categoryLabel = new JLabel("<html>Category: <b>" + project.getCategory() + "</b></html>"); JLabel categoryLabel = new JLabel("<html>Category: <b>" + project.getCategory() + "</b></html>");
JLabel authorLabel = new JLabel("<html>Author: <b>" + project.getOwner() + "</b></html>"); JLabel authorLabel = new JLabel("<html>Author: <b>" + project.getOwner() + "</b></html>");
JLabel updatedLabel = new JLabel("<html>Last updated: <b>" + project.getUpdated() + "</b></html>"); JLabel updatedLabel = new JLabel("<html>Last updated: <b>" + project.getUpdated() + "</b></html>");
@@ -271,9 +285,7 @@ public class ResultsScrollPanel extends JScrollPane {
gbc.insets = new Insets(2, 2, 2, 2); gbc.insets = new Insets(2, 2, 2, 2);
getResultsPanel().add(new JSeparator(), gbc); getResultsPanel().add(new JSeparator(), gbc);
getResultsPanel().invalidate(); return nameLabel;
return thumbnailLabel;
} }
private JLabel getLoadMoreLabel() { private JLabel getLoadMoreLabel() {
@@ -1,14 +1,17 @@
package org.diylc.swing.plugins.statusbar; package org.diylc.swing.plugins.statusbar;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point; import java.awt.Point;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.geom.Point2D; import java.awt.geom.Point2D;
import java.io.IOException;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.text.Format; import java.text.Format;
import java.util.ArrayList; import java.util.ArrayList;
@@ -27,6 +30,8 @@ import javax.swing.JPanel;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.diylc.announcements.Announcement;
import org.diylc.announcements.AnnouncementProvider;
import org.diylc.appframework.miscutils.ConfigurationManager; import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.appframework.miscutils.Utils; import org.diylc.appframework.miscutils.Utils;
import org.diylc.common.BadPositionException; import org.diylc.common.BadPositionException;
@@ -34,7 +39,9 @@ import org.diylc.common.ComponentType;
import org.diylc.common.EventType; import org.diylc.common.EventType;
import org.diylc.common.IPlugIn; import org.diylc.common.IPlugIn;
import org.diylc.common.IPlugInPort; import org.diylc.common.IPlugInPort;
import org.diylc.common.ITask;
import org.diylc.core.IDIYComponent; import org.diylc.core.IDIYComponent;
import org.diylc.core.IView;
import org.diylc.images.IconLoader; import org.diylc.images.IconLoader;
import org.diylc.presenter.Presenter; import org.diylc.presenter.Presenter;
import org.diylc.swing.ISwingUI; import org.diylc.swing.ISwingUI;
@@ -53,11 +60,13 @@ public class StatusBar extends JPanel implements IPlugIn {
private JComboBox zoomBox; private JComboBox zoomBox;
private UpdateLabel updateLabel; private UpdateLabel updateLabel;
private JLabel announcementLabel;
private MemoryBar memoryPanel; private MemoryBar memoryPanel;
private JLabel statusLabel; private JLabel statusLabel;
private JLabel sizeLabel; private JLabel sizeLabel;
private IPlugInPort plugInPort; private IPlugInPort plugInPort;
private ISwingUI swingUI;
// State variables // State variables
private ComponentType componentSlot; private ComponentType componentSlot;
@@ -67,8 +76,13 @@ public class StatusBar extends JPanel implements IPlugIn {
private List<String> stuckComponentNames; private List<String> stuckComponentNames;
private String statusMessage; private String statusMessage;
private AnnouncementProvider announcementProvider;
public StatusBar(ISwingUI swingUI) { public StatusBar(ISwingUI swingUI) {
super(); super();
this.swingUI = swingUI;
this.announcementProvider = new AnnouncementProvider();
setLayout(new GridBagLayout()); setLayout(new GridBagLayout());
@@ -77,6 +91,29 @@ public class StatusBar extends JPanel implements IPlugIn {
} catch (BadPositionException e) { } catch (BadPositionException e) {
LOG.error("Could not install status bar", e); LOG.error("Could not install status bar", e);
} }
swingUI.executeBackgroundTask(new ITask<String>() {
@Override
public String doInBackground() throws Exception {
Thread.sleep(1000);
return announcementProvider.getCurrentAnnouncements();
}
@Override
public void failed(Exception e) {
LOG.error("Error while fetching announcements", e);
}
@Override
public void complete(String result) {
if (result != null && result.length() > 0) {
StatusBar.this.swingUI.showMessage(result, "Public Announcements", IView.INFORMATION_MESSAGE);
announcementProvider.dismissed();
}
}
});
} }
private JComboBox getZoomBox() { private JComboBox getZoomBox() {
@@ -107,6 +144,45 @@ public class StatusBar extends JPanel implements IPlugIn {
return updateLabel; return updateLabel;
} }
public JLabel getAnnouncementLabel() {
if (announcementLabel == null) {
announcementLabel = new JLabel(IconLoader.Megaphone.getIcon());
announcementLabel.setToolTipText("Check for public announcements");
announcementLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
announcementLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
swingUI.executeBackgroundTask(new ITask<String>() {
@Override
public String doInBackground() throws Exception {
return announcementProvider.getCurrentAnnouncements();
}
@Override
public void failed(Exception e) {
LOG.error("Error while fetching announcements", e);
swingUI.showMessage("Could not fetch public announcements.", "Error", IView.ERROR_MESSAGE);
}
@Override
public void complete(String result) {
if (result != null && result.length() > 0) {
swingUI.showMessage(result, "Public Announcements", IView.INFORMATION_MESSAGE);
announcementProvider.dismissed();
} else
swingUI.showMessage("No new public announcements available.", "Public Announcements",
IView.INFORMATION_MESSAGE);
}
});
}
});
}
return announcementLabel;
}
private MemoryBar getMemoryPanel() { private MemoryBar getMemoryPanel() {
if (memoryPanel == null) { if (memoryPanel == null) {
memoryPanel = new MemoryBar(false); memoryPanel = new MemoryBar(false);
@@ -165,19 +241,25 @@ public class StatusBar extends JPanel implements IPlugIn {
// 4))); // 4)));
zoomPanel.setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4)); zoomPanel.setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4));
gbc.gridx = 1; gbc.gridx++;
gbc.fill = GridBagConstraints.BOTH; gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0; gbc.weightx = 0;
add(zoomPanel, gbc); add(zoomPanel, gbc);
gbc.gridx = 2; gbc.gridx++;
gbc.insets = new Insets(0, 2, 0, 2);
add(getSizeLabel(), gbc); add(getSizeLabel(), gbc);
gbc.gridx = 3; gbc.gridx++;
add(getAnnouncementLabel(), gbc);
gbc.gridx++;
gbc.insets = new Insets(0, 0, 0, 0);
add(getUpdateLabel(), gbc); add(getUpdateLabel(), gbc);
gbc.gridx = 4; gbc.gridx++;
gbc.fill = GridBagConstraints.NONE; gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(0, 0, 0, 4);
add(getMemoryPanel(), gbc); add(getMemoryPanel(), gbc);
gbc.gridx = 5; gbc.gridx = 5;