Skip to content

Commit

Permalink
Added documentation tool tips to editor tree, increased font size.
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyPants committed Dec 29, 2015
1 parent 4cd8fef commit 39d3afe
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package crazypants.structures.creator.block;

import java.awt.Font;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
Expand All @@ -8,15 +9,18 @@
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Enumeration;

import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.plaf.FontUIResource;

import org.apache.commons.lang3.StringUtils;
import org.lwjgl.input.Mouse;
Expand All @@ -30,9 +34,23 @@
public abstract class AbstractResourceDialog extends JDialog {

private static final long serialVersionUID = 1L;

private FileNameExtensionFilter filter;

static {
setUIFont (new FontUIResource("Dialog.plain",Font.PLAIN, 14));
}

private static void setUIFont(javax.swing.plaf.FontUIResource f) {
Enumeration<Object> keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if(value != null && value instanceof javax.swing.plaf.FontUIResource)
UIManager.put(key, f);
}
}

private FileNameExtensionFilter filter;

protected AbstractResourceDialog() {
setModal(false);
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolTip;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
Expand Down Expand Up @@ -143,7 +145,18 @@ public Dimension getPreferredSize() {

};

tree = new JTree(new DefaultMutableTreeNode());
tree = new JTree(new DefaultMutableTreeNode()) {

private static final long serialVersionUID = 1L;

@Override
public JToolTip createToolTip() {
MultiLineToolTip tip = new MultiLineToolTip();
tip.setColumns(25);
tip.setComponent(this);
return tip;
}
};
tree.setRootVisible(false);
tree.setEditable(false);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
Expand All @@ -156,7 +169,7 @@ private void addComponents() {
rootPan = new JPanel(new BorderLayout());
rootPan.add(editorPan, BorderLayout.SOUTH);
JScrollPane sp = new JScrollPane(tree);
sp.setPreferredSize(new Dimension(360, Math.min(Minecraft.getMinecraft().displayHeight - 20, 500)));
sp.setPreferredSize(new Dimension(400, Math.min(Minecraft.getMinecraft().displayHeight - 20, 500)));
rootPan.add(sp, BorderLayout.CENTER);

}
Expand All @@ -171,6 +184,8 @@ public void valueChanged(TreeSelectionEvent e) {
}

});

ToolTipManager.sharedInstance().registerComponent(tree);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package crazypants.structures.creator.block.tree;

import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.CellRendererPane;
import javax.swing.JComponent;
import javax.swing.JTextArea;
import javax.swing.JToolTip;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicToolTipUI;

public class MultiLineToolTip extends JToolTip {

private static final long serialVersionUID = 1L;

private int columns = 0;

private int fixedwidth = 0;

public MultiLineToolTip() {
updateUI();
}

@Override
public void updateUI() {
setUI(MultiLineToolTipUI.createUI(this));
}

public void setColumns(int columns) {
this.columns = columns;
this.fixedwidth = 0;
}

public int getColumns() {
return columns;
}

public void setFixedWidth(int width) {
this.fixedwidth = width;
this.columns = 0;
}

public int getFixedWidth() {
return fixedwidth;
}

}

class MultiLineToolTipUI extends BasicToolTipUI {

private static MultiLineToolTipUI sharedInstance = new MultiLineToolTipUI();

private CellRendererPane rendererPane;

private JTextArea textArea;

public static ComponentUI createUI(JComponent c) {
return sharedInstance;
}

public MultiLineToolTipUI() {
}

@Override
public void installUI(JComponent c) {
super.installUI(c);
rendererPane = new CellRendererPane();
c.add(rendererPane);
}

@Override
public void uninstallUI(JComponent c) {
super.uninstallUI(c);
c.remove(rendererPane);
rendererPane = null;
}

@Override
public void paint(Graphics g, JComponent c) {
Dimension size = c.getSize();
textArea.setBackground(c.getBackground());
rendererPane.paintComponent(g, textArea, c, 1, 1, size.width - 1,
size.height - 1, true);
}

@Override
public Dimension getPreferredSize(JComponent c) {
String tipText = ((JToolTip) c).getTipText();
if(tipText == null) {
return new Dimension(0, 0);
}
textArea = new JTextArea(tipText);
textArea.setBorder(new EmptyBorder(2, 4, 2, 4));
rendererPane.removeAll();
rendererPane.add(textArea);
textArea.setWrapStyleWord(true);
int width = ((MultiLineToolTip) c).getFixedWidth();
int columns = ((MultiLineToolTip) c).getColumns();

if(columns > 0) {
textArea.setColumns(columns);
textArea.setSize(0, 0);
textArea.setLineWrap(true);
textArea.setSize(textArea.getPreferredSize());
} else if(width > 0) {
textArea.setLineWrap(true);
Dimension d = textArea.getPreferredSize();
d.width = width;
d.height++;
textArea.setSize(d);
} else
textArea.setLineWrap(false);

Dimension dim = textArea.getPreferredSize();

dim.height += 1;
dim.width += 1;
return dim;
}

@Override
public Dimension getMinimumSize(JComponent c) {
return getPreferredSize(c);
}

@Override
public Dimension getMaximumSize(JComponent c) {
return getPreferredSize(c);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ public Component getTreeCellRendererComponent(JTree tree, Object value, boolean

EditorTreeNode node = (EditorTreeNode) value;
NodeData nb = node.getData();
IAttributeAccessor aa = nb.getAttributeAccessor();

String text = "";
if(leaf) {
if(nb.getAttributeAccessor() != null) {
if(nb.getAttributeAccessor() instanceof FieldAccessor) {
text = nb.getAttributeAccessor().getAttribuiteName();
if(aa != null) {
if(aa instanceof FieldAccessor) {
text = aa.getAttribuiteName();
}
if(nb.getValue() != null) {
if(text.length() > 0) {
Expand All @@ -58,8 +59,8 @@ public Component getTreeCellRendererComponent(JTree tree, Object value, boolean
text = getValueString(nb.getValue());
}
} else {
if(nb.getAttributeAccessor() != null) {
if(nb.getAttributeAccessor() instanceof FieldAccessor) {
if(aa != null) {
if(aa instanceof FieldAccessor) {
text = nb.getAttributeAccessor().getAttribuiteName() + ": ";
}
if(nb.getValue() != null && !(nb.getValue() instanceof Collection)) {
Expand All @@ -70,15 +71,24 @@ public Component getTreeCellRendererComponent(JTree tree, Object value, boolean
}
}
setText(text);

Icon icon = getIcon(nb);
if(icon != null) {
setIcon(icon);
} else if(leaf) {
setIcon(Icons.DOT);
}

if(aa != null && aa.getDocumentation() != null) {
setToolTipText(aa.getDocumentation());
//setToolTipText("Hello ppoppole\nluts");
} else {
setToolTipText(null);
}

return this;
}

private Icon getIcon(NodeData nd) {
if(nd == null || nd.getType() == null) {
return null;
Expand Down

0 comments on commit 39d3afe

Please sign in to comment.