All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.viaoa.jfc.editor.html.view.FontDialog Maven / Gradle / Ivy

/*  Copyright 1999-2015 Vince Via [email protected]
    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 com.viaoa.jfc.editor.html.view;

import java.util.*;

import java.io.*;
import java.awt.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import java.awt.print.*;
import java.net.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.event.*;
// import javax.help.*;

import com.viaoa.jfc.*;
import com.viaoa.jfc.text.view.OpenList;

public class FontDialog extends JDialog
{
	protected boolean m_succeeded = false;
	protected OpenList m_lstFontName;
	protected OpenList m_lstFontSize;
	protected MutableAttributeSet m_attributes;
	protected JCheckBox m_chkBold;
	protected JCheckBox m_chkItalic;
	protected JCheckBox m_chkUnderline;

	protected JCheckBox m_chkStrikethrough;
	protected JCheckBox m_chkSubscript;
	protected JCheckBox m_chkSuperscript;

	protected JComboBox m_cbColor;
	protected JLabel m_preview;
	private int[] sizes;

	public FontDialog(Window parent, String[] names, int[] sizes)
	{
		super(parent, "Font", ModalityType.APPLICATION_MODAL);
		this.sizes = sizes;
		this.setResizable(true);
		
		
        JPanel px = new JPanel(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();
        Insets ins = new Insets(1, 1, 1, 1);
        gc.insets = ins;
        gc.anchor = gc.NORTHWEST;
        gc.fill = gc.BOTH;

        gc.gridwidth = gc.REMAINDER;
        px.setBorder(new EmptyBorder(5,5,5,5));

        
		JPanel pp = new JPanel();
		pp.setBorder(new EmptyBorder(5,5,5,5));
		pp.setLayout(new BoxLayout(pp, BoxLayout.Y_AXIS));

		JPanel p = new JPanel(new GridLayout(1, 2, 10, 2));
		p.setBorder(new TitledBorder(new EtchedBorder(), "Font"));
		m_lstFontName = new OpenList(names, "Name:");
		p.add(m_lstFontName);
		
		m_lstFontSize = new OpenList(sizes, "Size:");
		p.add(m_lstFontSize);

        gc.fill = gc.BOTH;
        gc.weightx = 1.0;
        gc.weighty = 1.0;
		px.add(p, gc);
		// pp.add(p);

		p = new JPanel(new GridLayout(2, 3, 10, 5));
		p.setBorder(new TitledBorder(new EtchedBorder(), "Effects"));
		m_chkBold = new JCheckBox("Bold");
		p.add(m_chkBold);
		m_chkItalic = new JCheckBox("Italic");
		p.add(m_chkItalic);
		m_chkUnderline = new JCheckBox("Underline");
		p.add(m_chkUnderline);
		m_chkStrikethrough = new JCheckBox("Strikeout");
		p.add(m_chkStrikethrough);
		m_chkSubscript = new JCheckBox("Subscript");
		p.add(m_chkSubscript);
		m_chkSuperscript = new JCheckBox("Superscript");
		p.add(m_chkSuperscript);
		
        gc.weighty = .0;
        gc.fill = gc.HORIZONTAL;
        px.add(p, gc);
		//pp.add(p);
		//pp.add(Box.createVerticalStrut(5));

		p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
		p.add(Box.createHorizontalStrut(10));
		p.add(new JLabel("Color:"));
		p.add(Box.createHorizontalStrut(20));
		m_cbColor = new JComboBox();

		int[] values = new int[] { 0, 128, 192, 255 };
		for (int r=0; r= sizes.length) size = 12;
            else size = sizes[pos];
        }
		// StyleConstants.setFontSize(m_attributes, size);
        //vv: need to set in "pt" 
		m_attributes.addAttribute(StyleConstants.FontSize, size+"pt");  		
		
		StyleConstants.setBold(m_attributes,
			m_chkBold.isSelected());
		StyleConstants.setItalic(m_attributes,
			m_chkItalic.isSelected());
		StyleConstants.setUnderline(m_attributes,
			m_chkUnderline.isSelected());
		StyleConstants.setStrikeThrough(m_attributes,
			m_chkStrikethrough.isSelected());
		StyleConstants.setSubscript(m_attributes,
			m_chkSubscript.isSelected());
		StyleConstants.setSuperscript(m_attributes,
			m_chkSuperscript.isSelected());
		StyleConstants.setForeground(m_attributes, (Color)m_cbColor.getSelectedItem());
		return m_attributes;
	}


	public boolean succeeded() {
		return m_succeeded;
	}

    @Override
    public void setVisible(boolean aFlag) {
        m_succeeded = false;        
        super.setVisible(aFlag);
    }
	
	
	protected void updatePreview() {
		String name = m_lstFontName.getSelected();
		
		int size = m_lstFontSize.getSelectedInt();
		if (size <= 0) {
	        int pos = m_lstFontSize.getSelectedIndex();
	        if (pos < 0 || pos >= sizes.length) return;
		    size = sizes[pos];
		}
		int style = Font.PLAIN;
		if (m_chkBold.isSelected())
			style |= Font.BOLD;
		if (m_chkItalic.isSelected())
			style |= Font.ITALIC;

		// Bug Alert! This doesn't work if only style is changed.
		Font fn = new Font(name, style, size);
		m_preview.setFont(fn);

		Color c = (Color)m_cbColor.getSelectedItem();
		m_preview.setForeground(c);
		m_preview.repaint();
	}
}

class ColorComboRenderer extends JPanel implements ListCellRenderer
{
	protected Color m_color = Color.black;
	protected Color m_focusColor =
		(Color) UIManager.get("List.selectionBackground");
	protected Color m_nonFocusColor = Color.white;

	public Component getListCellRendererComponent(JList list,
	 Object obj, int row, boolean sel, boolean hasFocus)
	{
		if (hasFocus || sel)
			setBorder(new CompoundBorder(
				new MatteBorder(2, 10, 2, 10, m_focusColor),
				new LineBorder(Color.black)));
		else
			setBorder(new CompoundBorder(
				new MatteBorder(2, 10, 2, 10, m_nonFocusColor),
				new LineBorder(Color.black)));

		if (obj instanceof Color)
			m_color = (Color) obj;
		return this;
	}

	public void paintComponent(Graphics g) {
		setBackground(m_color);
		super.paintComponent(g);
	}
	
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy