NewsPanel.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.plugin.news |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.plugin.news;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import org.apache.log4j.Logger;
import org.xnap.XNap;
import org.xnap.gui.StatusBar;
import org.xnap.gui.component.EraseButton;
import org.xnap.gui.component.HTMLEditorPane;
import org.xnap.gui.component.HistoryComboBox;
import org.xnap.gui.component.XNapButton;
import org.xnap.gui.component.XNapIconButton;
import org.xnap.gui.util.GUIHelper;
import org.xnap.gui.util.GridBagHelper;
import org.xnap.gui.util.IconHelper;
import org.xnap.util.Formatter;
/**
*/
public class NewsPanel extends JPanel {
//--- Constant(s) ---
//--- Data field(s) ---
private static Logger logger = Logger.getLogger(NewsPanel.class);
private List newsItems = new LinkedList();
private HTMLEditorPane pane;
private JPanel locationPanel;
private JPanel actionPanel;
private List actionItems = new LinkedList();
private int cursorPos;
private StringBuffer content;
private HistoryComboBox jcbSearch;
private Action backAction = new BackAction();
private Action forwardAction = new ForwardAction();
private Action reloadAction = new ReloadAction();
private Action stopAction = new StopAction();
private Action queryAction = new QueryAction();
//--- Constructor(s) ---
public NewsPanel()
{
setLayout(new GridBagLayout());
// logo
JLabel jlLogo = new JLabel(IconHelper.getScaledLogo(48));
// set a small border on top
jlLogo.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
jlLogo.setHorizontalAlignment(SwingConstants.RIGHT);
GridBagHelper.add(this, jlLogo);
// html pane
pane = new HTMLEditorPane();
content = new StringBuffer();
HTMLEditorPane.addHeader(content);
content.append("<table margin=\"5\"><tr><td>");
cursorPos = content.length();
content.append("</td></tr></table>");
HTMLEditorPane.addFooter(content);
GridBagHelper.addPanel(this, new JScrollPane(pane));
// location panel
JPanel locationPanel = new JPanel(new GridBagLayout());
GridBagHelper.add(this, locationPanel);
EraseButton jbErase = new EraseButton();
GridBagHelper.addComponent(locationPanel, jbErase, GridBagConstraints.WEST);
locationPanel.add(new JLabel(XNap.tr("Location", 1)));
jcbSearch = new HistoryComboBox(20);
jcbSearch.setText("news:");
jcbSearch.setPreferences("news");
GUIHelper.bindEnterKeyLocally(jcbSearch.getTextField(), queryAction);
jbErase.setTextField(jcbSearch.getTextField());
GridBagHelper.addComponent(locationPanel, jcbSearch, GridBagConstraints.WEST);
GridBagHelper.addComponent(locationPanel, new XNapIconButton(queryAction));
GridBagHelper.addComponent(locationPanel, new XNapIconButton(backAction));
GridBagHelper.addComponent(locationPanel, new XNapIconButton(forwardAction));
GridBagHelper.addComponent(locationPanel, new XNapIconButton(reloadAction));
GridBagHelper.addComponent(locationPanel, new XNapIconButton(stopAction));
// actions
actionPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
GridBagHelper.add(locationPanel, actionPanel);
}
//--- Method(s) ---
public void add(NewsItem item)
{
if (!newsItems.contains(item)) {
newsItems.add(item);
String message
= "<b>" + item.getTitle() + "</b> - <i>"
+ item.getAuthor() + "</i> - <i>"
+ Formatter.formatDate(item.getDate()) + "</i>"
+ "<p>" + item.getDescription() + "</p>"
+ "<hr size=1><br>";
insert(message);
Action[] actions = item.getActions();
if (actions != null) {
for (int i = 0; i < actions.length; i++) {
if (!actionItems.contains(actions[i])) {
actionItems.add(actions[i]);
actionPanel.add(new XNapButton(actions[i]));
}
}
}
// logger.debug("news item added: " + item.toString());
// this.add(new NewsItemPanel(item));
// this.add(new JSeparator());
// this.add(Box.createVerticalStrut(10));
}
else {
logger.debug("news item already exists ("+item.toString()+").");
}
}
public void insert(String text)
{
content.insert(cursorPos, text);
cursorPos += text.length();
if (actionPanel.isVisible()) {
pane.setText(content.toString());
pane.setCaretPosition(0);
}
}
public boolean open(String location)
{
if (location.length() > 0) {
if (location.startsWith("news:")) {
pane.setText(content.toString());
actionPanel.setVisible(true);
return true;
}
if (location.indexOf(":") == -1) {
// prepend missing protocol
location = (location.startsWith(File.separator))
? "file:"
: "http://"
+ location;
}
try {
pane.setPage(location);
actionPanel.setVisible(false);
StatusBar.setText(XNap.tr("Retrieving {0}", location));
return true;
}
catch (IOException e) {
StatusBar.setText(e.getLocalizedMessage());
}
}
return false;
}
public class NewsItemPanel extends JPanel {
public NewsItemPanel(NewsItem item)
{
setLayout(new BorderLayout());
setBackground(Color.white);
String title
= "<html><b>" + item.getTitle() + "</b> - <i>"
+ item.getAuthor() + "</i> - <i>"
+ Formatter.formatDate(item.getDate()) + "</i>";
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
buttonPanel.setBackground(Color.white);
Action[] actions = item.getActions();
if (actions != null) {
for (int i = 0; i < actions.length; i++) {
buttonPanel.add(new XNapButton(actions[i]));
}
}
JLabel titleLabel = new JLabel(title);
JLabel descriptionLabel
= new JLabel(GUIHelper.tt(item.getDescription(), 450));
descriptionLabel.setBorder(GUIHelper.createEmptyBorder(10));
this.add(titleLabel, BorderLayout.NORTH);
this.add(descriptionLabel, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
}
}
//--- Inner Classes ---
// /**
// * Compares {@link NewsItem} objects by date.
// */
// public static class NewsItemComparator implements Comparator
// {
// public int compare(Object o1, Object o2)
// {
// return ((NewsItem)o1).getDate().compare
// (((NewsItem)o2).getDate());
// }
// }
/**
*
*/
private class QueryAction extends AbstractAction {
public QueryAction()
{
//putValue(Action.NAME, XNap.tr("Go"));
putValue(Action.SHORT_DESCRIPTION, XNap.tr("Perform search"));
putValue(IconHelper.XNAP_ICON, "key_enter.png");
}
public void actionPerformed(ActionEvent event)
{
String location = jcbSearch.getText().trim();
if (open(location)) {
jcbSearch.addDistinctItemAtTop(location);
}
}
}
/**
* Goes back in the browsing history
*/
private class BackAction extends AbstractAction
{
public BackAction()
{
putValue(Action.NAME, XNap.tr("Back"));
putValue(Action.SHORT_DESCRIPTION, XNap.tr("Go back"));
putValue(IconHelper.XNAP_ICON, "1leftarrow.png");
}
public void actionPerformed(ActionEvent event)
{
int i = jcbSearch.getSelectedIndex();
if (i != -1 && i < jcbSearch.getItemCount() - 1) {
jcbSearch.setSelectedIndex(i + 1);
open((String)jcbSearch.getItemAt(i + 1));
}
}
}
/**
* Goes forward in the browsing history
*/
private class ForwardAction extends AbstractAction
{
public ForwardAction()
{
putValue(Action.NAME, XNap.tr("Forward"));
putValue(Action.SHORT_DESCRIPTION, XNap.tr("Go forward"));
putValue(IconHelper.XNAP_ICON, "1rightarrow.png");
}
public void actionPerformed(ActionEvent event)
{
int i = jcbSearch.getSelectedIndex();
if (i > 0) {
jcbSearch.setSelectedIndex(i - 1);
open((String)jcbSearch.getItemAt(i - 1));
}
}
}
/**
* Reloads the current document
*/
private class ReloadAction extends AbstractAction
{
public ReloadAction()
{
putValue(Action.NAME, XNap.tr("Reload"));
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Reload current document"));
putValue(IconHelper.XNAP_ICON, "reload.png");
}
public void actionPerformed(ActionEvent event)
{
int i = jcbSearch.getSelectedIndex();
if (i != -1) {
open((String)jcbSearch.getItemAt(i));
}
}
}
/**
* Stops loading the current document
*/
private class StopAction extends AbstractAction
{
public StopAction()
{
putValue(Action.NAME, XNap.tr("Stop"));
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Stop loading the document"));
putValue(IconHelper.XNAP_ICON, "stop.png");
// FIX: currently not supported
setEnabled(false);
}
public void actionPerformed(ActionEvent event)
{
}
}
}
The table below shows all metrics for NewsPanel.java.




