/**
* Copyright 1999-2009 The Pegadi Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pegadi.storysketch.views;
import org.jgraph.JGraph;
import org.jgraph.graph.*;
import org.pegadi.storysketch.StorySketchPanel;
import org.pegadi.storysketch.cells.Archive;
import org.pegadi.storysketch.cells.ArchiveCell;
import org.pegadi.storysketch.views.plugins.ArchiveSearchPanel;
import org.pegadi.storysketch.views.plugins.UDArchiveSearchPanel;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.EventObject;
public class ArchiveView extends VertexView {
static ArchiveRenderer renderer = new ArchiveRenderer();
static ArchiveEditor editor = null;
public ArchiveView(Object cell, JGraph graph,CellMapper cm) {
super(cell, graph, cm);
}
// Returns the Renderer for this View
public CellViewRenderer getRenderer() {
return renderer;
}
public GraphCellEditor getEditor() {
if(editor == null)
editor = new ArchiveEditor();
return editor;
}
}
// Define the Renderer for an ArchiveView
class ArchiveRenderer implements CellViewRenderer {
JPanel component = new JPanel();
JLabel label = new JLabel("Arkiv");
RDFItem[] items = new RDFItem[0];
JTextArea textArea = new JTextArea();
public ArchiveRenderer() {
component.setOpaque(false);
component.setBorder(new Border() {
int FLIP_HEIGHT = 8;
int CORNER = 14;
public void paintBorder(Component c,
Graphics g,
int x,
int y,
int width,
int height) {
int FLIP_WIDTH = width/4;
// First, draw Flip
Polygon flip = new Polygon();
flip.addPoint(x,y);
flip.addPoint(x+FLIP_WIDTH,y);
flip.addPoint(x+FLIP_WIDTH+(int)(FLIP_HEIGHT*1.5), y+FLIP_HEIGHT);
flip.addPoint(x, y+FLIP_HEIGHT);
g.setColor(c.getBackground());
g.fillPolygon(flip);
g.setColor(Color.BLACK);
g.drawPolygon(flip);
// Draw rest of frame
g.setColor(c.getBackground());
g.fillRect(x, y+FLIP_HEIGHT, width-1, height-1-FLIP_HEIGHT);
g.setColor(Color.BLACK);
g.drawRect(x, y+FLIP_HEIGHT, width-1, height-1-FLIP_HEIGHT);
// "Open" flip
g.setColor(c.getBackground());
g.drawLine(x+1,y+ FLIP_HEIGHT,x + FLIP_WIDTH+(int)(FLIP_HEIGHT*1.5)-1, y + FLIP_HEIGHT);
}
public Insets getBorderInsets(Component c) {
return new Insets(FLIP_HEIGHT +3, 3, 3, 3);
}
public boolean isBorderOpaque() {
return false;
}
});
textArea.setFont(textArea.getFont().deriveFont((float)10));
textArea.setBackground(new Color(235, 235, 195));
component.setBackground(textArea.getBackground());
component.setLayout(new BorderLayout());
component.add(textArea, BorderLayout.CENTER);
}
public Component getRendererComponent(JGraph graph,
CellView view,
boolean sel,
boolean focus,
boolean preview) {
ArchiveCell ac = (ArchiveCell) view.getCell();
Archive a = (Archive) ac.getUserObject();
items = a.getRDFItems();
StringBuffer sb = new StringBuffer();
for (RDFItem item : items) {
sb.append("\u2022 ").append(item.getTitle()).append("\n");
}
if(items.length == 0) {
sb.append("\u2022 Arkivsaker");
}
textArea.setText(sb.toString());
return component;
}
}
class ArchiveEditor extends StorySketchCellEditor {
public ArchiveEditor() {
super();
}
protected GraphCellEditor createGraphCellEditor() {
return new ArchiveEditor.RealCellEditor();
}
class RealCellEditor extends AbstractCellEditor implements GraphCellEditor {
JPanel editorComponent = new JPanel();
ArchiveCell ac;
ArchiveSearchPanel search = new UDArchiveSearchPanel();
//DefaultListModel model = new DefaultListModel();
RDFTableModel model = new RDFTableModel();
RDFTable articles = new RDFTable(model,true);
JEditorPane editor = new JEditorPane();
JScrollPane articlesScroll;
public RealCellEditor() {
articles.setTransferHandler(new ArticleListTransferHandler());
MouseListener ml = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
articles_mouseClicked(e);
}
};
articles.addMouseListener(ml);
search.getResultTable().addMouseListener(ml);
editor.setPreferredSize(new Dimension(300,200));
editor.setEditable(false);
editor.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
editor_hyperlinkUpdate(e);
}
});
editorComponent.setLayout(new BorderLayout());
editorComponent.setBorder(UIManager.getBorder("Tree.editorBorder"));
Box box = Box.createVerticalBox();
JLabel archiveLabel = new JLabel("Arkivsaker");
archiveLabel.setFont(archiveLabel.getFont().deriveFont((float)16));
JPanel labelPanel = new JPanel();
labelPanel.add(archiveLabel);
box.add(labelPanel);
box.add(Box.createVerticalStrut(10));
// Add help text
JLabel helpLabel = new JLabel("<html><center>Her kan søke i arkivet etter relevante saker.<br>Dobbeltklikk pa saken for a lese den.<br><br>Eksempelsøk:<br> <b>journalist:klungtveit AND tittel:student.no</b></center></html>");
helpLabel.setFont(helpLabel.getFont().deriveFont((float)11));
JPanel helpPanel = new JPanel();
helpPanel.add(helpLabel);
box.add(helpPanel);
box.add(Box.createVerticalStrut(10));
JLabel label = new JLabel("<html>Dra saker du vil huske hit:</html>");
JPanel right = new JPanel();
right.setLayout(new BorderLayout());
right.add(label, BorderLayout.NORTH);
right.add(new JScrollPane(articles), BorderLayout.CENTER);
JSplitPane topSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
search,
right);
topSplit.setDividerLocation(0.8);
JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
topSplit,
new JScrollPane(editor));
editorComponent.add(box, BorderLayout.NORTH);
editorComponent.add(split, BorderLayout.CENTER);
}
public void editor_hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
if (e.getURL().toString().startsWith("http://underdusken.no:8888")) {
try {
editor.setPage(e.getURL());
} catch (Exception ex) {
JOptionPane.showMessageDialog(SwingUtilities.windowForComponent(editor), "Kunne ikke laste side",
"Feil ved lasting",
JOptionPane.ERROR_MESSAGE);
}
} else {
if(StorySketchPanel.launcher != null)
StorySketchPanel.launcher.browserLaunch(e.getURL().toString());
}
}
}
void articles_mouseClicked(MouseEvent e) {
Object source = e.getSource();
if(!(source instanceof RDFTable)) {
return;
}
RDFTable table = (RDFTable) source;
final RDFItem item = (RDFItem) table.getValueAt(table.getSelectedRow(), 0);
if(SwingUtilities.isRightMouseButton(e)) {
JPopupMenu p = new JPopupMenu();
p.add(new AbstractAction("Fjern fra lista") {
public void actionPerformed(ActionEvent e) {
model.removeItem(item);
}
});
p.show(articles, e.getX(), e.getY());
return;
}
if(e.getClickCount() == 2) {
if(item != null) {
try {
table.setCursor(new Cursor(Cursor.WAIT_CURSOR));
editor.setPage(item.getID());
table.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
} catch (Exception ex) {
JOptionPane.showMessageDialog(SwingUtilities.windowForComponent(table), "Kunne ikke laste artikkelen",
"Feil ved lasting",
JOptionPane.ERROR_MESSAGE);
table.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
ex.printStackTrace();
}
}
}
}
public Component getGraphCellEditorComponent(JGraph graph,
Object value,
boolean isSelected) {
// Clear current model and add from the archive beeing edited
ac = (ArchiveCell) editingCell;
Archive a = (Archive) ac.getUserObject();
RDFItem[] items = a.getRDFItems();
model.clear();
for (RDFItem item : items) {
model.addItem(item);
}
search.clear();
editor.setText("");
return editorComponent;
}
public Object getCellEditorValue() {
return ac;
}
public boolean stopCellEditing() {
ac = (ArchiveCell) graph.getEditingCell();
Archive a = (Archive) ac.getUserObject();
RDFItem[] items = new RDFItem[model.getRowCount()];
for(int i = 0; i < items.length; i++) {
items[i] = (RDFItem) model.getValueAt(i,0);
}
a.setRDFItems(items);
return true;
}
public boolean shouldSelectCell(EventObject event) {
//textArea.requestFocus();
return super.shouldSelectCell(event);
}
}
}