/**
* 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 java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
public class RDFList extends JList {
public RDFList(ListModel model, boolean simple) {
super(model);
if(!simple)
setCellRenderer(new RDFItemCellRenderer());
}
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(200,250);
}
class RDFItemCellRenderer extends JPanel implements ListCellRenderer {
JLabel titleLabel = new JLabel();
JLabel descriptionLabel = new JLabel();
JLabel dateLabel = new JLabel();
public RDFItemCellRenderer() {
titleLabel = new JLabel();
descriptionLabel = new JLabel();
dateLabel = new JLabel();
setLayout(new GridLayout(1,1));
Box box = Box.createVerticalBox();
//add(titleLabel, BorderLayout.NORTH);
box.add(titleLabel);
box.add(Box.createVerticalStrut(10));
//add(descriptionLabel, BorderLayout.CENTER);
box.add(descriptionLabel);
box.add(Box.createVerticalStrut(3));
//add(dateLabel, BorderLayout.SOUTH);
box.add(dateLabel);
box.add(Box.createVerticalStrut(10));
add(box);
titleLabel.setFont(titleLabel.getFont().deriveFont((float)12).deriveFont(Font.BOLD));
descriptionLabel.setFont(descriptionLabel.getFont().deriveFont((float)11));
dateLabel.setFont(dateLabel.getFont().deriveFont((float)10));
}
// This is the only method defined by ListCellRenderer.
// We just reconfigure the JLabel each time we're called.
public Component getListCellRendererComponent(JList list,
Object value, // value to display
int index, // cell index
boolean isSelected, // is the cell selected
boolean cellHasFocus) // the list and the cell have the focus
{
if(value instanceof RDFItem) {
RDFItem item = (RDFItem) value;
titleLabel.setText("<html>" +item.getTitle() +"</html>");
descriptionLabel.setText("<html>" +item.getDescription() +"</html>");
dateLabel.setText(item.getDate());
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
}
else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setEnabled(list.isEnabled());
setFont(list.getFont());
setOpaque(true);
}
return this;
}
/*
public Dimension getPreferredSize() {
return box.getPreferredSize();
}
*/
}
public boolean getScrollableTracksViewportWidth() {
return true;
}
}