get rid of fixed base panel size

This commit is contained in:
Ekaterina Tuzova
2014-10-17 18:19:05 +04:00
parent 269d489e0f
commit a89aa320b7
6 changed files with 85 additions and 27 deletions
@@ -11,7 +11,7 @@ import net.sourceforge.jeuclid.context.LayoutContextImpl;
import net.sourceforge.jeuclid.context.Parameter;
import net.sourceforge.jeuclid.converter.Converter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.ipnb.editor.IpnbEditorUtil;
import org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel;
import org.jetbrains.plugins.ipnb.editor.panels.IpnbTexPackageDefinitions;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
@@ -26,6 +26,8 @@ import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
import javax.xml.parsers.ParserConfigurationException;
import java.awt.*;
import java.awt.event.HierarchyBoundsAdapter;
import java.awt.event.HierarchyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
@@ -42,8 +44,7 @@ public class IpnbUtils {
private static final String ourImagePrefix = "http:\\image";
private static final Font ourFont = new Font(Font.SERIF, Font.PLAIN, 16);
private static final String ourBodyRule = "body { font-family: \"DejaVu\"; " +
"font-size: " + ourFont.getSize() + "pt; " +
"width: " + IpnbEditorUtil.PANEL_WIDTH + "px;}";
"font-size: " + ourFont.getSize() + "pt;}";
private static final String ourCodeRule = "code { font-family: \"DejaVu\"; " +
"font-size: " + ourFont.getSize() + "pt;}";
@@ -88,6 +89,7 @@ public class IpnbUtils {
final String html = convertToHtml(source, editorPane);
editorPane.setText("<html><body>" + html + "</body></html>");
editorPane.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
@@ -97,9 +99,33 @@ public class IpnbUtils {
});
editorPane.addHyperlinkListener(new BrowserHyperlinkListener());
//TODO: jump to the section (see User Interface#Utilities)
editorPane.addHierarchyBoundsListener(new IpnbHierarchyBoundsAdapter(editorPane));
panel.add(editorPane);
}
public static class IpnbHierarchyBoundsAdapter extends HierarchyBoundsAdapter {
private final JComponent myComponent;
public IpnbHierarchyBoundsAdapter(@NotNull final JComponent component) {
myComponent = component;
}
@Override
public void ancestorResized(HierarchyEvent e) {
final Component component = e.getChanged();
if (component instanceof IpnbFilePanel) {
final int width = component.getWidth();
if (width > 0) {
myComponent.setPreferredSize(new Dimension(width - 300, myComponent.getPreferredSize().height));
myComponent.revalidate();
myComponent.repaint();
}
}
}
}
private static String convertToHtml(@NotNull final String source, @NotNull final JEditorPane editorPane) {
final StringBuilder result = new StringBuilder();
StringBuilder markdown = new StringBuilder();
@@ -44,7 +44,6 @@ public class IpnbEditorUtil {
public enum PromptType { In, Out, None }
public static Dimension PROMPT_SIZE = new Dimension(80, 30);
public static int PANEL_WIDTH = (int)(Toolkit.getDefaultToolkit().getScreenSize().width * 0.5);
public static Editor createPythonCodeEditor(@NotNull final Project project, @NotNull final IpnbCodeSourcePanel codeSourcePanel) {
final EditorFactory editorFactory = EditorFactory.getInstance();
@@ -6,14 +6,14 @@ import com.intellij.ui.Gray;
import com.intellij.ui.JBColor;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.ipnb.IpnbUtils;
import org.jetbrains.plugins.ipnb.format.cells.IpnbEditableCell;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Utilities;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.*;
public abstract class IpnbEditablePanel<T extends JComponent, K extends IpnbEditableCell> extends IpnbPanel<T, K> {
private static final Logger LOG = Logger.getInstance(IpnbEditablePanel.class);
@@ -84,6 +84,24 @@ public abstract class IpnbEditablePanel<T extends JComponent, K extends IpnbEdit
private JTextArea createEditablePanel() {
final JTextArea textArea = new JTextArea(getRawCellText());
addHierarchyBoundsListener(new IpnbUtils.IpnbHierarchyBoundsAdapter(this));
textArea.addHierarchyBoundsListener(new IpnbUtils.IpnbHierarchyBoundsAdapter(textArea));
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
final int height = Math.max(textArea.getFontMetrics(getFont()).getHeight() * getLineCount(textArea),
myViewPanel.getPreferredSize().height);
setPreferredSize(new Dimension(textArea.getWidth(), height));
final Container parent = getParent();
if (parent instanceof IpnbFilePanel) {
IpnbFilePanel filePanel = (IpnbFilePanel)parent;
filePanel.revalidate();
filePanel.repaint();
}
}
});
textArea.setLineWrap(true);
textArea.setEditable(true);
textArea.setBorder(BorderFactory.createLineBorder(JBColor.lightGray));
@@ -104,7 +122,17 @@ public abstract class IpnbEditablePanel<T extends JComponent, K extends IpnbEdit
});
textArea.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
public void keyReleased(KeyEvent e) {
final int height = textArea.getFontMetrics(getFont()).getHeight() * getLineCount(textArea);
final Dimension preferredSize = myViewPanel.getPreferredSize();
setPreferredSize(new Dimension(preferredSize.width, Math.max(height, preferredSize.height)));
textArea.setPreferredSize(new Dimension(textArea.getWidth(), height));
textArea.revalidate();
textArea.repaint();
revalidate();
repaint();
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
setEditing(false);
final Container parent = getParent();
@@ -118,6 +146,22 @@ public abstract class IpnbEditablePanel<T extends JComponent, K extends IpnbEdit
return textArea;
}
public int getLineCount(@NotNull final JTextArea textArea) {
int totalCharacters = textArea.getText().length();
int lineCount = 1;
try {
int offset = totalCharacters;
while (offset > 0) {
offset = Utilities.getRowStart(textArea, offset) - 1;
lineCount++;
}
} catch (BadLocationException e) {
return 1;
}
return lineCount;
}
public boolean contains(int y) {
return y>= getTop() && y<=getBottom();
}
@@ -155,11 +155,6 @@ public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider {
c.gridwidth = 1;
c.insets = new Insets(INSET_Y, INSET_X, 0, 0);
if (myIpnbPanels.isEmpty()) {
final int width = IpnbEditorUtil.PANEL_WIDTH + IpnbEditorUtil.PROMPT_SIZE.width;
final JLabel label = new JLabel("<html><body style='width: " + width + "px'></body></html>");
add(label, c);
}
final IpnbEditablePanel selectedCell = getSelectedCell();
final int index = myIpnbPanels.indexOf(selectedCell);
myIpnbFile.addCell(cell, index + 1);
@@ -18,7 +18,7 @@ public class IpnbHeadingPanel extends IpnbEditablePanel<JBLabel, IpnbHeadingCell
}
private String renderCellText() {
return "<html><body style='width: " + IpnbEditorUtil.PANEL_WIDTH + "px'><h" + myCell.getLevel() + ">" + myCell.getSourceAsString() + "</h" + myCell.getLevel() +
return "<html><body><h" + myCell.getLevel() + ">" + myCell.getSourceAsString() + "</h" + myCell.getLevel() +
"></body></html>";
}
@@ -80,9 +80,13 @@ public class IpnbCodeSourcePanel extends IpnbPanel<JComponent, IpnbCodeCell> imp
@Override
public void keyReleased(KeyEvent e) {
final int keyCode = e.getKeyCode();
final int height = myEditor.getLineHeight() * Math.max(myEditor.getDocument().getLineCount(), 1);
component.setPreferredSize(new Dimension(IpnbEditorUtil.PANEL_WIDTH, height));
final Container parent = myParent.getParent();
final int height = myEditor.getLineHeight() * Math.max(myEditor.getDocument().getLineCount(), 1) + 5;
contentComponent.setPreferredSize(new Dimension(parent.getWidth() - 300, height));
myParent.revalidate();
myParent.repaint();
if (parent instanceof IpnbFilePanel) {
IpnbFilePanel ipnbFilePanel = (IpnbFilePanel)parent;
ipnbFilePanel.revalidate();
@@ -115,16 +119,6 @@ public class IpnbCodeSourcePanel extends IpnbPanel<JComponent, IpnbCodeCell> imp
panel.add(component);
component.addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
@Override
public void ancestorResized(HierarchyEvent e) {
if (e.getChanged() instanceof IpnbFilePanel) {
myParent.setPreferredSize(new Dimension(e.getChanged().getWidth() - 200, myParent.getPreferredSize().height));
}
}
});
component.setPreferredSize(new Dimension(IpnbEditorUtil.PANEL_WIDTH, component.getPreferredSize().height));
component.setMinimumSize(new Dimension(100, component.getPreferredSize().height));
return panel;
}
}