List of usage examples for java.awt FontMetrics getHeight
public int getHeight()
From source file:org.multibit.viewsystem.swing.view.panels.ShowPreferencesPanel.java
private JPanel createAppearancePanel(int stentWidth) { MultiBitTitledPanel appearancePanel = new MultiBitTitledPanel( controller.getLocaliser().getString("showPreferencesPanel.appearanceTitle"), ComponentOrientation.getOrientation(controller.getLocaliser().getLocale())); GridBagConstraints constraints = new GridBagConstraints(); constraints.fill = GridBagConstraints.BOTH; constraints.gridx = 0;//www.j a v a 2 s. c o m constraints.gridy = 3; constraints.weightx = 0.1; constraints.weighty = 0.3; constraints.gridwidth = 1; constraints.gridheight = 1; constraints.anchor = GridBagConstraints.LINE_START; JPanel indent = MultiBitTitledPanel.getIndentPanel(1); appearancePanel.add(indent, constraints); constraints.fill = GridBagConstraints.BOTH; constraints.gridx = 1; constraints.gridy = 4; constraints.weightx = 0.3; constraints.weighty = 0.3; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_START; JPanel stent = MultiBitTitledPanel.createStent(stentWidth); appearancePanel.add(stent, constraints); constraints.fill = GridBagConstraints.BOTH; constraints.gridx = 2; constraints.gridy = 3; constraints.weightx = 0.05; constraints.weighty = 0.3; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.CENTER; appearancePanel.add( MultiBitTitledPanel.createStent(MultiBitTitledPanel.SEPARATION_BETWEEN_NAME_VALUE_PAIRS), constraints); MultiBitLabel fontNameLabel = new MultiBitLabel( controller.getLocaliser().getString("fontChooser.fontName")); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 1; constraints.gridy = 4; constraints.weightx = 0.3; constraints.weighty = 1; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_END; appearancePanel.add(fontNameLabel, constraints); fontNameTextLabel = new MultiBitLabel(""); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 3; constraints.gridy = 4; constraints.weightx = 0.3; constraints.weighty = 1; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_START; appearancePanel.add(fontNameTextLabel, constraints); MultiBitLabel fontStyleLabel = new MultiBitLabel( controller.getLocaliser().getString("fontChooser.fontStyle")); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 1; constraints.gridy = 5; constraints.weightx = 0.3; constraints.weighty = 1; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_END; appearancePanel.add(fontStyleLabel, constraints); fontStyleTextLabel = new MultiBitLabel(""); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 3; constraints.gridy = 5; constraints.weightx = 0.3; constraints.weighty = 1; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_START; appearancePanel.add(fontStyleTextLabel, constraints); MultiBitLabel fontSizeLabel = new MultiBitLabel( controller.getLocaliser().getString("fontChooser.fontSize")); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 1; constraints.gridy = 6; constraints.weightx = 0.3; constraints.weighty = 1; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_END; appearancePanel.add(fontSizeLabel, constraints); fontSizeTextLabel = new MultiBitLabel(""); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 3; constraints.gridy = 6; constraints.weightx = 0.3; constraints.weighty = 1; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_START; appearancePanel.add(fontSizeTextLabel, constraints); ChooseFontAction chooseFontAction = new ChooseFontAction(controller, this, null); MultiBitButton fontChooserButton = new MultiBitButton(chooseFontAction, controller); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 3; constraints.gridy = 7; constraints.weightx = 1; constraints.weighty = 1; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_START; appearancePanel.add(fontChooserButton, constraints); constraints.fill = GridBagConstraints.VERTICAL; constraints.gridx = 4; constraints.gridy = 8; constraints.weightx = 0.2; constraints.weighty = 0.3; constraints.gridwidth = 3; constraints.anchor = GridBagConstraints.LINE_START; appearancePanel.add(MultiBitTitledPanel.createStent(1, 30), constraints); MultiBitLabel lookAndFeelLabel = new MultiBitLabel( controller.getLocaliser().getString("showPreferencesPanel.lookAndFeel")); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 1; constraints.gridy = 9; constraints.weightx = 0.3; constraints.weighty = 1; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_END; appearancePanel.add(lookAndFeelLabel, constraints); originalLookAndFeel = controller.getModel().getUserPreference(CoreModel.LOOK_AND_FEEL); LookAndFeelInfo[] lookAndFeels = UIManager.getInstalledLookAndFeels(); lookAndFeelComboBox = new JComboBox(); lookAndFeelComboBox.addItem(localisedSystemLookAndFeelName); if (lookAndFeels != null) { for (LookAndFeelInfo info : lookAndFeels) { lookAndFeelComboBox.addItem(info.getName()); if (info.getName().equalsIgnoreCase(originalLookAndFeel)) { lookAndFeelComboBox.setSelectedItem(info.getName()); } } } if (originalLookAndFeel == null || originalLookAndFeel.equals("") || CoreModel.SYSTEM_LOOK_AND_FEEL.equalsIgnoreCase(originalLookAndFeel)) { lookAndFeelComboBox.setSelectedItem(localisedSystemLookAndFeelName); } lookAndFeelComboBox.setFont(FontSizer.INSTANCE.getAdjustedDefaultFont()); lookAndFeelComboBox.setOpaque(false); FontMetrics fontMetrics = getFontMetrics(FontSizer.INSTANCE.getAdjustedDefaultFont()); int textWidth = Math.max(fontMetrics.stringWidth("CDE/Motif"), fontMetrics.stringWidth("Windows classic")); Dimension preferredSize = new Dimension(textWidth + TICKER_COMBO_WIDTH_DELTA, fontMetrics.getHeight() + EXCHANGE_COMBO_HEIGHT_DELTA); lookAndFeelComboBox.setPreferredSize(preferredSize); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 3; constraints.gridy = 9; constraints.weightx = 0.8; constraints.weighty = 0.6; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_START; appearancePanel.add(lookAndFeelComboBox, constraints); JPanel fill1 = new JPanel(); fill1.setOpaque(false); constraints.fill = GridBagConstraints.BOTH; constraints.gridx = 4; constraints.gridy = 10; constraints.weightx = 20; constraints.weighty = 1; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_END; appearancePanel.add(fill1, constraints); return appearancePanel; }
From source file:spectrogram.Spectrogram.java
/** * Define the size and position of different parts of image and add global labels *///from w w w . j a va 2 s . co m private void initImage() { colorModel = IndexColorTables.getColorTable(color); // use anti-aliasing font representation System.setProperty("awt.useSystemAAFontSettings", "on"); System.setProperty("swing.aatext", "true"); // img contains the output image with all the labels img = new BufferedImage(outX, outY, BufferedImage.TYPE_BYTE_INDEXED, colorModel); grph = img.createGraphics(); grph.setBackground(Color.white); grph.clearRect(0, 0, outX, outY); String fontName = getAGoodFont(); lblFont = new Font(fontName, Font.PLAIN, 16); titleFont = new Font(fontName, Font.BOLD, 18); FontMetrics titleMetrics = grph.getFontMetrics(titleFont); labelMetrics = grph.getFontMetrics(lblFont); em = labelMetrics.stringWidth("M"); String title; String utcDate; String hrTime = TimeAndDate.hrTime(duration); String cname; if (useAltData) { cname = "Test Data"; if (testDataFilename != null && !testDataFilename.isEmpty()) { File f = new File(testDataFilename); cname = f.getName(); } if (rawDataFilename != null && !rawDataFilename.isEmpty()) { File f = new File(rawDataFilename); cname = f.getName(); } if (channelName != null && !channelName.isEmpty()) { cname = channelName; } if (startGPS == 0) { Date now = new Date(); startGPS = (int) TimeAndDate.utc2gps(now.getTime() / 1000); } } else { cname = channelName; } utcDate = TimeAndDate.gpsAsUtcString(startGPS); title = String.format("%1$s %2$s - %3$,d (%4$s)", cname, utcDate, startGPS, hrTime); titleHeight = titleMetrics.getHeight(); int titleWidth = titleMetrics.stringWidth(title); lblHeight = labelMetrics.getHeight(); int lblWidth = labelMetrics.stringWidth("99,999"); int tbMargin = 10; int lrMargin = 10; // plot title int tx = outX / 2 - titleWidth / 2; int ty = (int) Math.round(lblHeight * 1.5); grph.setPaint(Color.BLACK); grph.setFont(titleFont); grph.drawString(title, tx, ty); // calc and draw color map int cmWidth = 48; int cmLblWidth = labelMetrics.stringWidth("0.000000") + em; int cmLeft = outX - lrMargin * 2 - titleHeight - cmLblWidth - cmWidth; imgY0 = titleHeight + tbMargin * 2; dimY = outY - imgY0 - lblHeight * 5 - tbMargin; cmRect = new Rectangle(cmLeft, imgY0, cmWidth, dimY); grph.drawRect(cmLeft, imgY0, cmWidth, dimY); // calc and draw image rectangle imgX0 = lrMargin * 2 + titleHeight + lblWidth; dimX = cmLeft - imgX0 - lrMargin; pltRect = new Rectangle(imgX0, imgY0, dimX, dimY); grph.drawRect(imgX0, imgY0, dimX, dimY); // y-axis label String leftAxisLabel = "Frequency (Hz)"; grph.setColor(Color.BLACK); tx = lblHeight + lrMargin; titleWidth = titleMetrics.stringWidth(leftAxisLabel); ty = outY / 2 + titleWidth / 2; drawRotatedText(tx, ty, -90, leftAxisLabel); // color map label String rightAxisLabel = scaling.toString(); if (norm) { rightAxisLabel += " Normalized."; } tx = outX - lblHeight - lrMargin; titleWidth = titleMetrics.stringWidth(rightAxisLabel); ty = outY / 2 + titleWidth / 2; drawRotatedText(tx, ty, -90, rightAxisLabel); int nsamples = (int) (duration * sampleRate); colPerSample = 1; if (nsamples < dimX) { // we have more pixels available than we have samples so making it pretty takes some work colPerSample = dimX / nsamples; dimX = colPerSample * nsamples; } rast = img.getRaster(); fillColorMap(); }
From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractTable.java
protected void applyFont(JTable table, Font font) { Graphics graphics = table.getGraphics(); if (graphics != null) { FontMetrics metrics = graphics.getFontMetrics(font); defaultRowHeight = metrics.getHeight() + DEFAULT_ROW_MARGIN; fontInitialized = true;//w ww .jav a2 s .co m if (impl != null) { packRows(); } } }
From source file:com.projity.contrib.calendar.JXXMonthView.java
/** * Calculates size information necessary for laying out the month view. *//*from www . j av a 2 s . c o m*/ private void update() { // Loop through year and get largest representation of the month. // Keep track of the longest month so we can loop through it to // determine the width of a date box. int currDays; int longestMonth = 0; int daysInLongestMonth = 0; int currWidth; int longestMonthWidth = 0; // We use a bold font for figuring out size constraints since // it's larger and flaggedDates will be noted in this style. _derivedFont = getFont().deriveFont(Font.BOLD); _baselineFont = getFont().deriveFont(Font.HANGING_BASELINE); FontMetrics fm = getFontMetrics(_derivedFont); _cal.set(Calendar.MONTH, _cal.getMinimum(Calendar.MONTH)); _cal.set(Calendar.DAY_OF_MONTH, _cal.getActualMinimum(Calendar.DAY_OF_MONTH)); for (int i = 0; i < _cal.getMaximum(Calendar.MONTH); i++) { currWidth = fm.stringWidth(_monthsOfTheYear[i]); if (currWidth > longestMonthWidth) { longestMonthWidth = currWidth; } currDays = _cal.getActualMaximum(Calendar.DAY_OF_MONTH); if (currDays > daysInLongestMonth) { longestMonth = _cal.get(Calendar.MONTH); daysInLongestMonth = currDays; } _cal.add(Calendar.MONTH, 1); } // Loop through longest month and get largest representation of the day // of the month. _cal.set(Calendar.MONTH, longestMonth); _cal.set(Calendar.DAY_OF_MONTH, _cal.getActualMinimum(Calendar.DAY_OF_MONTH)); _boxHeight = fm.getHeight(); for (int i = 0; i < daysInLongestMonth; i++) { currWidth = fm.stringWidth(_dayOfMonthFormatter.format(_cal.getTime())); if (currWidth > _boxWidth) { _boxWidth = currWidth; } _cal.add(Calendar.DAY_OF_MONTH, 1); } // Modify _boxWidth if month string is longer _dim.width = (_boxWidth + (2 * _boxPaddingX)) * DAYS_IN_WEEK; if (_dim.width < longestMonthWidth) { double diff = longestMonthWidth - _dim.width; _boxWidth += Math.ceil(diff / (double) DAYS_IN_WEEK); _dim.width = (_boxWidth + (2 * _boxPaddingX)) * DAYS_IN_WEEK; } // Keep track of calendar width and height for use later. _calendarWidth = (_boxWidth + (2 * _boxPaddingX)) * DAYS_IN_WEEK; _calendarHeight = (_boxPaddingY + _boxHeight + _boxPaddingY) * 8; // Calculate minimum width/height for the component. _dim.height = (_calendarHeight * _minCalRows) + (CALENDAR_SPACING * (_minCalRows - 1)); _dim.width = (_calendarWidth * _minCalCols) + (CALENDAR_SPACING * (_minCalCols - 1)); // Add insets to the dimensions. Insets insets = getInsets(); _dim.width += insets.left + insets.right; _dim.height += insets.top + insets.bottom; // Restore calendar. _cal.setTimeInMillis(_firstDisplayedDate); }
From source file:org.multibit.viewsystem.swing.view.panels.ShowPreferencesPanel.java
private JPanel createTickerPanel(int stentWidth) { // Load up the original values. originalShowTicker = !Boolean.FALSE.toString() .equals(controller.getModel().getUserPreference(ExchangeModel.TICKER_SHOW)); originalExchange1 = controller.getModel().getUserPreference(ExchangeModel.TICKER_FIRST_ROW_EXCHANGE); originalCurrency1 = controller.getModel().getUserPreference(ExchangeModel.TICKER_FIRST_ROW_CURRENCY); // Map MtGox to Bitstamp + USD if (ExchangeData.MT_GOX_EXCHANGE_NAME.equalsIgnoreCase(originalExchange1)) { originalExchange1 = ExchangeData.BITSTAMP_EXCHANGE_NAME; controller.getModel().setUserPreference(ExchangeModel.TICKER_FIRST_ROW_EXCHANGE, ExchangeData.BITSTAMP_EXCHANGE_NAME); originalCurrency1 = "USD"; controller.getModel().setUserPreference(ExchangeModel.TICKER_FIRST_ROW_CURRENCY, "USD"); }/* w ww . j a v a 2 s. c om*/ originalShowSecondRow = Boolean.TRUE.toString() .equals(controller.getModel().getUserPreference(ExchangeModel.TICKER_SHOW_SECOND_ROW)); originalExchange2 = controller.getModel().getUserPreference(ExchangeModel.TICKER_SECOND_ROW_EXCHANGE); originalCurrency2 = controller.getModel().getUserPreference(ExchangeModel.TICKER_SECOND_ROW_CURRENCY); // Map MtGox to Bitstamp if (ExchangeData.MT_GOX_EXCHANGE_NAME.equalsIgnoreCase(originalExchange2)) { originalExchange2 = ExchangeData.BITSTAMP_EXCHANGE_NAME; controller.getModel().setUserPreference(ExchangeModel.TICKER_SECOND_ROW_EXCHANGE, ExchangeData.BITSTAMP_EXCHANGE_NAME); originalCurrency2 = "USD"; controller.getModel().setUserPreference(ExchangeModel.TICKER_SECOND_ROW_CURRENCY, "USD"); } MultiBitTitledPanel tickerPanel = new MultiBitTitledPanel( controller.getLocaliser().getString("showPreferencesPanel.ticker.title2"), ComponentOrientation.getOrientation(controller.getLocaliser().getLocale())); GridBagConstraints constraints = new GridBagConstraints(); constraints.fill = GridBagConstraints.BOTH; constraints.gridx = 3; constraints.gridy = 3; constraints.weightx = 0.05; constraints.weighty = 0.3; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.CENTER; tickerPanel.add(MultiBitTitledPanel.createStent(MultiBitTitledPanel.SEPARATION_BETWEEN_NAME_VALUE_PAIRS), constraints); String showTickerText = controller.getLocaliser().getString("multiBitFrame.ticker.show.text"); if (showTickerText != null && showTickerText.length() >= 1) { // Capitalise text (this is to save adding a new I18n term. showTickerText = Character.toUpperCase(showTickerText.charAt(0)) + showTickerText.toLowerCase().substring(1); } showTicker = new JCheckBox(showTickerText); showTicker.setOpaque(false); showTicker.setFont(FontSizer.INSTANCE.getAdjustedDefaultFont()); showTicker.setSelected(originalShowTicker); exchangeInformationLabel = new MultiBitLabel( controller.getLocaliser().getString("showPreferencesPanel.ticker.exchangeInformation")); exchangeInformationLabel.setVisible(originalShowBitcoinConvertedToFiat); showBitcoinConvertedToFiat = new JCheckBox( controller.getLocaliser().getString("showPreferencesPanel.ticker.showBitcoinConvertedToFiat")); showBitcoinConvertedToFiat.setOpaque(false); showBitcoinConvertedToFiat.setFont(FontSizer.INSTANCE.getAdjustedDefaultFont()); showBitcoinConvertedToFiat.setSelected(originalShowBitcoinConvertedToFiat); showBitcoinConvertedToFiat.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { boolean selectedChange = (e.getStateChange() == ItemEvent.SELECTED); boolean unSelectedChange = (e.getStateChange() == ItemEvent.DESELECTED); if (exchangeInformationLabel != null) { if (selectedChange) { exchangeInformationLabel.setVisible(true); } if (unSelectedChange) { exchangeInformationLabel.setVisible(false); } } } }); showExchange = new JCheckBox(controller.getLocaliser().getString("tickerTableModel.exchange")); showExchange.setOpaque(false); showExchange.setFont(FontSizer.INSTANCE.getAdjustedDefaultFont()); showCurrency = new JCheckBox(controller.getLocaliser().getString("tickerTableModel.currency")); showCurrency.setOpaque(false); showCurrency.setFont(FontSizer.INSTANCE.getAdjustedDefaultFont()); showLastPrice = new JCheckBox(controller.getLocaliser().getString("tickerTableModel.lastPrice")); showLastPrice.setOpaque(false); showLastPrice.setFont(FontSizer.INSTANCE.getAdjustedDefaultFont()); showBid = new JCheckBox(controller.getLocaliser().getString("tickerTableModel.bid")); showBid.setOpaque(false); showBid.setFont(FontSizer.INSTANCE.getAdjustedDefaultFont()); showAsk = new JCheckBox(controller.getLocaliser().getString("tickerTableModel.ask")); showAsk.setOpaque(false); showAsk.setFont(FontSizer.INSTANCE.getAdjustedDefaultFont()); String tickerColumnsToShow = controller.getModel().getUserPreference(ExchangeModel.TICKER_COLUMNS_TO_SHOW); if (tickerColumnsToShow == null || tickerColumnsToShow.equals("")) { tickerColumnsToShow = TickerTableModel.DEFAULT_COLUMNS_TO_SHOW; } originalShowCurrency = tickerColumnsToShow.contains(TickerTableModel.TICKER_COLUMN_CURRENCY); showCurrency.setSelected(originalShowCurrency); originalShowRate = tickerColumnsToShow.contains(TickerTableModel.TICKER_COLUMN_LAST_PRICE); showLastPrice.setSelected(originalShowRate); originalShowBid = tickerColumnsToShow.contains(TickerTableModel.TICKER_COLUMN_BID); showBid.setSelected(originalShowBid); originalShowAsk = tickerColumnsToShow.contains(TickerTableModel.TICKER_COLUMN_ASK); showAsk.setSelected(originalShowAsk); originalShowExchange = tickerColumnsToShow.contains(TickerTableModel.TICKER_COLUMN_EXCHANGE); showExchange.setSelected(originalShowExchange); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.gridx = 1; constraints.gridy = 4; constraints.weightx = 0.2; constraints.weighty = 0.3; constraints.gridwidth = 4; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(showTicker, constraints); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.gridx = 1; constraints.gridy = 5; constraints.weightx = 0.2; constraints.weighty = 0.3; constraints.gridwidth = 4; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(showBitcoinConvertedToFiat, constraints); constraints.fill = GridBagConstraints.VERTICAL; constraints.gridx = 0; constraints.gridy = 6; constraints.weightx = 0.2; constraints.weighty = 0.3; constraints.gridwidth = 3; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(MultiBitTitledPanel.createStent(1, 12), constraints); MultiBitTitledPanel.addLeftJustifiedTextAtIndent( controller.getLocaliser().getString("showPreferencesPanel.ticker.columnsToShow"), 7, tickerPanel); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 1; constraints.gridy = 8; constraints.weightx = 0.2; constraints.weighty = 0.3; constraints.gridwidth = 3; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(showExchange, constraints); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.gridx = 1; constraints.gridy = 9; constraints.weightx = 0.2; constraints.weighty = 0.3; constraints.gridwidth = 3; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(showCurrency, constraints); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 1; constraints.gridy = 10; constraints.weightx = 0.2; constraints.weighty = 0.3; constraints.gridwidth = 3; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(showLastPrice, constraints); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 1; constraints.gridy = 11; constraints.weightx = 0.2; constraints.weighty = 0.3; constraints.gridwidth = 3; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(showBid, constraints); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 1; constraints.gridy = 12; constraints.weightx = 0.2; constraints.weighty = 0.3; constraints.gridwidth = 3; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(showAsk, constraints); constraints.fill = GridBagConstraints.VERTICAL; constraints.gridx = 1; constraints.gridy = 13; constraints.weightx = 0.3; constraints.weighty = 0.3; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(MultiBitTitledPanel.createStent(1, 13), constraints); MultiBitTitledPanel.addLeftJustifiedTextAtIndent( controller.getLocaliser().getString("showPreferencesPanel.ticker.firstRow"), 14, tickerPanel); MultiBitLabel exchangeLabel1 = new MultiBitLabel( controller.getLocaliser().getString("showPreferencesPanel.ticker.exchange")); exchangeLabel1.setHorizontalAlignment(JLabel.TRAILING); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.gridx = 1; constraints.gridy = 15; constraints.weightx = 0.3; constraints.weighty = 1; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_END; tickerPanel.add(exchangeLabel1, constraints); String exchangeToUse1; if (originalExchange1 == null | "".equals(originalExchange1)) { exchangeToUse1 = ExchangeData.DEFAULT_EXCHANGE; } else { exchangeToUse1 = originalExchange1; } String exchangeToUse2; if (originalExchange2 == null | "".equals(originalExchange2)) { exchangeToUse2 = ExchangeData.DEFAULT_EXCHANGE; } else { exchangeToUse2 = originalExchange2; } exchangeComboBox1 = new JComboBox(ExchangeData.getAvailableExchanges()); exchangeComboBox1.setSelectedItem(exchangeToUse1); exchangeComboBox1.setFont(FontSizer.INSTANCE.getAdjustedDefaultFont()); exchangeComboBox1.setOpaque(false); FontMetrics fontMetrics = getFontMetrics(FontSizer.INSTANCE.getAdjustedDefaultFont()); int textWidth = Math.max(fontMetrics.stringWidth(ExchangeData.OPEN_EXCHANGE_RATES_EXCHANGE_NAME), fontMetrics.stringWidth("USD")) + COMBO_WIDTH_DELTA; Dimension preferredSize = new Dimension(textWidth + TICKER_COMBO_WIDTH_DELTA, fontMetrics.getHeight() + EXCHANGE_COMBO_HEIGHT_DELTA); exchangeComboBox1.setPreferredSize(preferredSize); constraints.fill = GridBagConstraints.BOTH; constraints.gridx = 1; constraints.gridy = 15; constraints.weightx = 0.3; constraints.weighty = 0.3; constraints.gridwidth = 2; constraints.anchor = GridBagConstraints.LINE_START; JPanel stent = MultiBitTitledPanel.createStent(stentWidth); tickerPanel.add(stent, constraints); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 4; constraints.gridy = 15; constraints.weightx = 0.8; constraints.weighty = 0.6; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(exchangeComboBox1, constraints); oerMessageLabel1 = new MultiBitLabel( " " + controller.getLocaliser().getString("showPreferencesPanel.getAppId.label")); oerMessageLabel1.setForeground(Color.GREEN.darker().darker()); boolean showMessageLabel1 = isBrowserSupported() && ExchangeData.OPEN_EXCHANGE_RATES_EXCHANGE_NAME.equalsIgnoreCase(exchangeToUse1) && (originalOERApiCode == null || originalOERApiCode.trim().length() == 0); oerMessageLabel1.setVisible(showMessageLabel1); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 5; constraints.gridy = 15; constraints.weightx = 0.8; constraints.weighty = 0.6; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(oerMessageLabel1, constraints); MultiBitLabel currencyLabel1 = new MultiBitLabel( controller.getLocaliser().getString("showPreferencesPanel.ticker.currency")); currencyLabel1.setHorizontalAlignment(JLabel.TRAILING); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.gridx = 1; constraints.gridy = 16; constraints.weightx = 0.3; constraints.weighty = 1; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_END; tickerPanel.add(currencyLabel1, constraints); // Make sure the exchange1 has been created and initialised the list of // currencies. if (mainFrame != null && mainFrame.getTickerTimerTask1() != null) { TickerTimerTask tickerTimerTask = mainFrame.getTickerTimerTask1(); synchronized (tickerTimerTask) { if (tickerTimerTask.getExchange() == null) { tickerTimerTask.createExchangeObjects(exchangeToUse1); } } } currencyComboBox1 = new JComboBox(); Collection<String> currenciesToUse = ExchangeData.getAvailableCurrenciesForExchange(exchangeToUse1); if (currenciesToUse != null) { for (String currency : currenciesToUse) { String item = currency; String description = CurrencyConverter.INSTANCE.getCurrencyCodeToDescriptionMap().get(currency); if (description != null && description.trim().length() > 0) { item = item + " (" + description + ")"; } currencyComboBox1.addItem(item); if (currency.equals(originalCurrency1)) { currencyComboBox1.setSelectedItem(item); } } } currencyComboBox1.setFont(FontSizer.INSTANCE.getAdjustedDefaultFont()); currencyComboBox1.setOpaque(false); currencyComboBox1.setPreferredSize(preferredSize); exchangeComboBox1.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent event) { if (event.getStateChange() == ItemEvent.SELECTED) { Object item = event.getItem(); String exchangeShortName = item.toString(); // Make sure the exchange1 has been created and initialised // the list of currencies. if (mainFrame != null && mainFrame.getTickerTimerTask1() != null) { TickerTimerTask tickerTimerTask = mainFrame.getTickerTimerTask1(); synchronized (tickerTimerTask) { tickerTimerTask.createExchangeObjects(exchangeShortName); currencyComboBox1.removeAllItems(); Collection<String> currenciesToUse = ExchangeData .getAvailableCurrenciesForExchange(exchangeShortName); if (currenciesToUse != null) { for (String currency : currenciesToUse) { String loopItem = currency; String description = CurrencyConverter.INSTANCE .getCurrencyCodeToDescriptionMap().get(currency); if (description != null && description.trim().length() > 0) { loopItem = loopItem + " (" + description + ")"; } currencyComboBox1.addItem(loopItem); } } } } // Enable the OpenExchangeRates App ID if required. boolean showOER = ExchangeData.OPEN_EXCHANGE_RATES_EXCHANGE_NAME .equalsIgnoreCase(exchangeShortName) || ExchangeData.OPEN_EXCHANGE_RATES_EXCHANGE_NAME .equalsIgnoreCase((String) exchangeComboBox2.getSelectedItem()); oerStent.setVisible(showOER); oerApiCodeLabel.setVisible(showOER); oerApiCodeTextField.setVisible(showOER); getOerAppIdButton.setVisible(showOER); boolean showMessageLabel = isBrowserSupported() && ExchangeData.OPEN_EXCHANGE_RATES_EXCHANGE_NAME.equalsIgnoreCase(exchangeShortName) && (oerApiCodeTextField.getText() == null || oerApiCodeTextField.getText().trim().length() == 0); oerMessageLabel1.setVisible(showMessageLabel); } } }); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 4; constraints.gridy = 16; constraints.weightx = 0.8; constraints.weighty = 0.6; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(currencyComboBox1, constraints); constraints.fill = GridBagConstraints.BOTH; constraints.gridx = 1; constraints.gridy = 17; constraints.weightx = 0.3; constraints.weighty = 0.3; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(MultiBitTitledPanel.createStent(12, 12), constraints); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 4; constraints.gridy = 18; constraints.weightx = 0.8; constraints.weighty = 0.6; constraints.gridwidth = 2; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(MultiBitTitledPanel.createStent(fontMetrics.stringWidth(exchangeInformationLabel.getText()), fontMetrics.getHeight()), constraints); tickerPanel.add(exchangeInformationLabel, constraints); constraints.fill = GridBagConstraints.BOTH; constraints.gridx = 1; constraints.gridy = 19; constraints.weightx = 0.3; constraints.weighty = 0.3; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(MultiBitTitledPanel.createStent(12, 12), constraints); MultiBitTitledPanel.addLeftJustifiedTextAtIndent( controller.getLocaliser().getString("showPreferencesPanel.ticker.secondRow"), 20, tickerPanel); showSecondRowCheckBox = new JCheckBox( controller.getLocaliser().getString("showPreferencesPanel.ticker.showSecondRow")); showSecondRowCheckBox.setOpaque(false); showSecondRowCheckBox.setFont(FontSizer.INSTANCE.getAdjustedDefaultFont()); showSecondRowCheckBox.addItemListener(new ChangeTickerShowSecondRowListener()); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.gridx = 1; constraints.gridy = 21; constraints.weightx = 0.3; constraints.weighty = 1; constraints.gridwidth = 3; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(showSecondRowCheckBox, constraints); exchangeLabel2 = new MultiBitLabel( controller.getLocaliser().getString("showPreferencesPanel.ticker.exchange")); exchangeLabel2.setHorizontalAlignment(JLabel.TRAILING); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.gridx = 1; constraints.gridy = 22; constraints.weightx = 0.3; constraints.weighty = 1; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_END; tickerPanel.add(exchangeLabel2, constraints); exchangeComboBox2 = new JComboBox(ExchangeData.getAvailableExchanges()); exchangeComboBox2.setSelectedItem(exchangeToUse2); exchangeComboBox2.setFont(FontSizer.INSTANCE.getAdjustedDefaultFont()); exchangeComboBox2.setOpaque(false); exchangeComboBox2.setPreferredSize(preferredSize); exchangeComboBox2.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent event) { if (event.getStateChange() == ItemEvent.SELECTED) { Object item = event.getItem(); String exchangeShortName = item.toString(); // Make sure the exchange2 has been created and initialised // the list of currencies. if (mainFrame != null && mainFrame.getTickerTimerTask2() != null) { TickerTimerTask tickerTimerTask = mainFrame.getTickerTimerTask2(); synchronized (tickerTimerTask) { tickerTimerTask.createExchangeObjects(exchangeShortName); currencyComboBox2.removeAllItems(); Collection<String> currenciesToUse = ExchangeData .getAvailableCurrenciesForExchange(exchangeShortName); if (currenciesToUse != null) { for (String currency : currenciesToUse) { String loopItem = currency; String description = CurrencyConverter.INSTANCE .getCurrencyCodeToDescriptionMap().get(currency); if (description != null && description.trim().length() > 0) { loopItem = loopItem + " (" + description + ")"; } currencyComboBox2.addItem(loopItem); } } } } // Enable the OpenExchangeRates App ID if required. boolean showOER = ExchangeData.OPEN_EXCHANGE_RATES_EXCHANGE_NAME .equalsIgnoreCase(exchangeShortName) || ExchangeData.OPEN_EXCHANGE_RATES_EXCHANGE_NAME .equalsIgnoreCase((String) exchangeComboBox1.getSelectedItem()); oerStent.setVisible(showOER); oerApiCodeLabel.setVisible(showOER); oerApiCodeTextField.setVisible(showOER); getOerAppIdButton.setVisible(showOER); boolean showMessageLabel = isBrowserSupported() && ExchangeData.OPEN_EXCHANGE_RATES_EXCHANGE_NAME.equalsIgnoreCase(exchangeShortName) && (oerApiCodeTextField.getText() == null || oerApiCodeTextField.getText().trim().length() == 0); oerMessageLabel2.setVisible(showMessageLabel); } } }); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 4; constraints.gridy = 22; constraints.weightx = 0.8; constraints.weighty = 0.6; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(exchangeComboBox2, constraints); oerMessageLabel2 = new MultiBitLabel( " " + controller.getLocaliser().getString("showPreferencesPanel.getAppId.label")); oerMessageLabel2.setForeground(Color.GREEN.darker().darker()); boolean showMessageLabel2 = isBrowserSupported() && ExchangeData.OPEN_EXCHANGE_RATES_EXCHANGE_NAME.equalsIgnoreCase(exchangeToUse2) && (originalOERApiCode == null || originalOERApiCode.trim().length() == 0); oerMessageLabel2.setVisible(showMessageLabel2); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 5; constraints.gridy = 22; constraints.weightx = 0.8; constraints.weighty = 0.6; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(oerMessageLabel2, constraints); currencyLabel2 = new MultiBitLabel( controller.getLocaliser().getString("showPreferencesPanel.ticker.currency")); currencyLabel2.setHorizontalAlignment(JLabel.TRAILING); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.gridx = 1; constraints.gridy = 23; constraints.weightx = 0.3; constraints.weighty = 1; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_END; tickerPanel.add(currencyLabel2, constraints); // Make sure the exchange2 has been created and initialised the list of // currencies. if (mainFrame != null && mainFrame.getTickerTimerTask2() != null) { TickerTimerTask tickerTimerTask = mainFrame.getTickerTimerTask2(); synchronized (tickerTimerTask) { if (tickerTimerTask.getExchange() == null) { tickerTimerTask.createExchangeObjects(exchangeToUse2); } } } currencyComboBox2 = new JComboBox(); currenciesToUse = ExchangeData.getAvailableCurrenciesForExchange(exchangeToUse2); if (currenciesToUse != null) { for (String currency : currenciesToUse) { String loopItem = currency; String description = CurrencyConverter.INSTANCE.getCurrencyCodeToDescriptionMap().get(currency); if (description != null && description.trim().length() > 0) { loopItem = loopItem + " (" + description + ")"; } currencyComboBox2.addItem(loopItem); if (currency.equals(originalCurrency2)) { currencyComboBox2.setSelectedItem(loopItem); } } } currencyComboBox2.setFont(FontSizer.INSTANCE.getAdjustedDefaultFont()); currencyComboBox2.setOpaque(false); currencyComboBox2.setPreferredSize(preferredSize); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 4; constraints.gridy = 23; constraints.weightx = 0.8; constraints.weighty = 0.6; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(currencyComboBox2, constraints); showSecondRowCheckBox.setSelected(originalShowSecondRow); enableTickerSecondRow(originalShowSecondRow); constraints.fill = GridBagConstraints.BOTH; constraints.gridx = 1; constraints.gridy = 24; constraints.weightx = 0.3; constraints.weighty = 0.3; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(MultiBitTitledPanel.createStent(12, 12), constraints); JPanel fill1 = new JPanel(); fill1.setOpaque(false); constraints.fill = GridBagConstraints.BOTH; constraints.gridx = 5; constraints.gridy = 25; constraints.weightx = 20; constraints.weighty = 1; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_END; tickerPanel.add(fill1, constraints); boolean showOerSignup = ExchangeData.OPEN_EXCHANGE_RATES_EXCHANGE_NAME.equalsIgnoreCase(exchangeToUse1) || ExchangeData.OPEN_EXCHANGE_RATES_EXCHANGE_NAME.equalsIgnoreCase(exchangeToUse2); oerStent = MultiBitTitledPanel.createStent(12, 12); constraints.fill = GridBagConstraints.BOTH; constraints.gridx = 1; constraints.gridy = 26; constraints.weightx = 0.3; constraints.weighty = 0.3; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_START; oerStent.setVisible(showOerSignup); tickerPanel.add(oerStent, constraints); oerApiCodeLabel = new MultiBitLabel( controller.getLocaliser().getString("showPreferencesPanel.oerLabel.text")); oerApiCodeLabel.setToolTipText(HelpContentsPanel .createTooltipText(controller.getLocaliser().getString("showPreferencesPanel.oerLabel.tooltip"))); oerApiCodeLabel.setVisible(showOerSignup); oerApiCodeTextField = new MultiBitTextField("", 25, controller); oerApiCodeTextField.setHorizontalAlignment(JLabel.LEADING); oerApiCodeTextField.setMinimumSize(new Dimension(API_CODE_FIELD_WIDTH, API_CODE_FIELD_HEIGHT)); oerApiCodeTextField.setPreferredSize(new Dimension(API_CODE_FIELD_WIDTH, API_CODE_FIELD_HEIGHT)); oerApiCodeTextField.setMaximumSize(new Dimension(API_CODE_FIELD_WIDTH, API_CODE_FIELD_HEIGHT)); oerApiCodeTextField.setVisible(showOerSignup); oerApiCodeTextField.setText(originalOERApiCode); oerApiCodeTextField.addFocusListener(new FocusListener() { @Override public void focusGained(FocusEvent arg0) { } @Override public void focusLost(FocusEvent arg0) { String apiCode = oerApiCodeTextField.getText(); if (apiCode != null && !(WhitespaceTrimmer.trim(apiCode).length() == 0) && !apiCode.equals( controller.getModel().getUserPreference(ExchangeModel.OPEN_EXCHANGE_RATES_API_CODE))) { // New API code. // Check its length if (!(apiCode.trim().length() == LENGTH_OF_OPEN_EXCHANGE_RATE_APP_ID) && !apiCode.equals(haveShownErrorMessageForApiCode)) { haveShownErrorMessageForApiCode = apiCode; // Give user a message that App ID is not in the correct format. JOptionPane.showMessageDialog(null, new String[] { controller .getLocaliser().getString("showPreferencesPanel.oerValidationError.text1"), " ", controller.getLocaliser() .getString("showPreferencesPanel.oerValidationError.text2"), controller.getLocaliser() .getString("showPreferencesPanel.oerValidationError.text3") }, controller.getLocaliser().getString( "showPreferencesPanel.oerValidationError.title"), JOptionPane.ERROR_MESSAGE, ImageLoader.createImageIcon(ImageLoader.EXCLAMATION_MARK_ICON_FILE)); return; } } updateApiCode(); } }); oerApiCodeTextField.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { String apiCode = oerApiCodeTextField.getText(); if (apiCode != null && !(WhitespaceTrimmer.trim(apiCode).length() == 0) && !apiCode.equals( controller.getModel().getUserPreference(ExchangeModel.OPEN_EXCHANGE_RATES_API_CODE))) { // New API code. // Check its length if (!(apiCode.trim().length() == LENGTH_OF_OPEN_EXCHANGE_RATE_APP_ID) && !apiCode.equals(haveShownErrorMessageForApiCode)) { haveShownErrorMessageForApiCode = apiCode; // Give user a message that App ID is not in the correct format. JOptionPane.showMessageDialog(null, new String[] { controller.getLocaliser() .getString("showPreferencesPanel.oerValidationError.text1"), controller.getLocaliser() .getString("showPreferencesPanel.oerValidationError.text2"), controller.getLocaliser() .getString("showPreferencesPanel.oerValidationError.text3") }, controller.getLocaliser().getString( "showPreferencesPanel.oerValidationError.title"), JOptionPane.ERROR_MESSAGE, ImageLoader.createImageIcon(ImageLoader.EXCLAMATION_MARK_ICON_FILE)); return; } } updateApiCode(); } }); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 1; constraints.gridy = 27; constraints.weightx = 0.3; constraints.weighty = 0.3; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_END; tickerPanel.add(oerApiCodeLabel, constraints); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 4; constraints.gridy = 27; constraints.weightx = 0.3; constraints.weighty = 0.3; constraints.gridwidth = 2; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(oerApiCodeTextField, constraints); if (isBrowserSupported()) { getOerAppIdButton = new MultiBitButton( controller.getLocaliser().getString("showPreferencesPanel.getAppId.text")); getOerAppIdButton .setToolTipText(controller.getLocaliser().getString("showPreferencesPanel.getAppId.tooltip")); getOerAppIdButton.setVisible(showOerSignup); getOerAppIdButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { try { openURI(new URI(OPEN_EXCHANGE_RATES_SIGN_UP_URI)); } catch (URISyntaxException e) { log.debug(e.getMessage()); } } }); constraints.fill = GridBagConstraints.NONE; constraints.gridx = 4; constraints.gridy = 28; constraints.weightx = 1; constraints.weighty = 1; constraints.gridwidth = 1; constraints.anchor = GridBagConstraints.LINE_START; tickerPanel.add(getOerAppIdButton, constraints); } return tickerPanel; }
From source file:org.yccheok.jstock.gui.charting.InvestmentFlowLayerUI.java
private void updateInvestInformationBox(Graphics2D g2) { final Font oldFont = g2.getFont(); final Font paramFont = oldFont; final FontMetrics paramFontMetrics = g2.getFontMetrics(paramFont); final Font valueFont = oldFont.deriveFont(oldFont.getStyle() | Font.BOLD, (float) oldFont.getSize() + 1); final FontMetrics valueFontMetrics = g2.getFontMetrics(valueFont); final Font dateFont = oldFont.deriveFont((float) oldFont.getSize() - 1); final FontMetrics dateFontMetrics = g2.getFontMetrics(dateFont); final Activities activities = this.investmentFlowChartJDialog.getInvestActivities(this.investPointIndex); this.investValues.clear(); this.investParams.clear(); this.totalInvestValue = 0.0; final DecimalPlace decimalPlace = JStock.instance().getJStockOptions().getDecimalPlace(); for (int i = 0, size = activities.size(); i < size; i++) { final Activity activity = activities.get(i); // Buy only. this.investParams.add(activity.getType() + " " + org.yccheok.jstock.portfolio.Utils.toQuantity(activity.get(Activity.Param.Quantity)) + " " + ((StockInfo) activity.get(Activity.Param.StockInfo)).symbol); if (activity.getType() == Activity.Type.Buy) { final StockInfo stockInfo = (StockInfo) activity.get(Activity.Param.StockInfo); final double amount = convertToPoundIfNecessary(stockInfo.code, activity.getAmount()); this.totalInvestValue += amount; this.investValues .add(org.yccheok.jstock.portfolio.Utils.toCurrencyWithSymbol(decimalPlace, amount)); } else {/*from w w w. j a v a2 s. co m*/ assert (false); } } final boolean isTotalNeeded = this.investParams.size() > 1; final String totalParam = GUIBundle.getString("InvestmentFlowLayerUI_Total_Invest"); final String totalValue = org.yccheok.jstock.portfolio.Utils.toCurrencyWithSymbol(decimalPlace, this.totalInvestValue); /* This is the height for "total" information. */ int totalHeight = 0; assert (this.investValues.size() == this.investParams.size()); int index = 0; final int paramValueWidthMargin = 10; final int paramValueHeightMargin = 0; int maxInfoWidth = -1; // paramFontMetrics will always "smaller" than valueFontMetrics. int totalInfoHeight = Math.max(paramFontMetrics.getHeight(), valueFontMetrics.getHeight()) * this.investValues.size() + paramValueHeightMargin * (this.investValues.size() - 1); for (String param : this.investParams) { final String value = this.investValues.get(index++); final int paramStringWidth = paramFontMetrics.stringWidth(param + ":") + paramValueWidthMargin + valueFontMetrics.stringWidth(value); if (maxInfoWidth < paramStringWidth) { maxInfoWidth = paramStringWidth; } } if (isTotalNeeded) { final int tmp = paramFontMetrics.stringWidth(totalParam + ":") + paramValueWidthMargin + valueFontMetrics.stringWidth(totalValue); if (maxInfoWidth < tmp) { maxInfoWidth = tmp; } totalHeight = Math.max(paramFontMetrics.getHeight(), valueFontMetrics.getHeight()); } final Date date = activities.getDate().getTime(); final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE, MMMM d, yyyy"); final String dateString = simpleDateFormat.format(date); final int dateStringWidth = dateFontMetrics.stringWidth(dateString); final int dateStringHeight = dateFontMetrics.getHeight(); final int maxStringWidth = Math.max(dateStringWidth, maxInfoWidth); final int dateInfoHeightMargin = 5; final int infoTotalHeightMargin = 5; final int maxStringHeight = isTotalNeeded ? (dateStringHeight + dateInfoHeightMargin + totalInfoHeight + infoTotalHeightMargin + totalHeight) : (dateStringHeight + dateInfoHeightMargin + totalInfoHeight); // Now, We have a pretty good information on maxStringWidth and maxStringHeight. final int padding = 5; final int boxPointMargin = 8; final int width = maxStringWidth + (padding << 1); final int height = maxStringHeight + (padding << 1); // On left side of the ball. final int suggestedX = (int) (this.investPoint.getX() - width - boxPointMargin); final int suggestedY = (int) (this.investPoint.getY() - (height >> 1)); final int x = suggestedX > this.drawArea.getX() ? (suggestedX + width) < (this.drawArea.getX() + this.drawArea.getWidth()) ? suggestedX : (int) (this.drawArea.getX() + this.drawArea.getWidth() - width - boxPointMargin) : (int) (investPoint.getX() + boxPointMargin); final int y = suggestedY > this.drawArea.getY() ? (suggestedY + height) < (this.drawArea.getY() + this.drawArea.getHeight()) ? suggestedY : (int) (this.drawArea.getY() + this.drawArea.getHeight() - height - boxPointMargin) : (int) (this.drawArea.getY() + boxPointMargin); this.investRect.setRect(x, y, width, height); }
From source file:org.yccheok.jstock.gui.charting.InvestmentFlowLayerUI.java
private void drawInformationBox(Graphics2D g2, Activities activities, Rectangle2D rect, List<String> params, List<String> values, String totalParam, double totalValue, Color background_color, Color border_color) { final Font oldFont = g2.getFont(); final Font paramFont = oldFont; final FontMetrics paramFontMetrics = g2.getFontMetrics(paramFont); final Font valueFont = oldFont.deriveFont(oldFont.getStyle() | Font.BOLD, (float) oldFont.getSize() + 1); final FontMetrics valueFontMetrics = g2.getFontMetrics(valueFont); final Font dateFont = oldFont.deriveFont((float) oldFont.getSize() - 1); final FontMetrics dateFontMetrics = g2.getFontMetrics(dateFont); final int x = (int) rect.getX(); final int y = (int) rect.getY(); final int width = (int) rect.getWidth(); final int height = (int) rect.getHeight(); final Object oldValueAntiAlias = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING); final Composite oldComposite = g2.getComposite(); final Color oldColor = g2.getColor(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(border_color);/*from ww w .j a v a 2s .c om*/ g2.drawRoundRect(x - 1, y - 1, width + 1, height + 1, 15, 15); g2.setColor(background_color); g2.setComposite(Utils.makeComposite(0.75f)); g2.fillRoundRect(x, y, width, height, 15, 15); g2.setComposite(oldComposite); g2.setColor(oldColor); final Date date = activities.getDate().getTime(); final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE, MMMM d, yyyy"); final String dateString = simpleDateFormat.format(date); final int padding = 5; int yy = y + padding + dateFontMetrics.getAscent(); g2.setFont(dateFont); g2.setColor(COLOR_BLUE); g2.drawString(dateString, ((width - dateFontMetrics.stringWidth(dateString)) >> 1) + x, yy); int index = 0; final int dateInfoHeightMargin = 5; final int infoTotalHeightMargin = 5; final int paramValueHeightMargin = 0; yy += dateFontMetrics.getDescent() + dateInfoHeightMargin + Math.max(paramFontMetrics.getAscent(), valueFontMetrics.getAscent()); for (String param : params) { final String value = values.get(index++); g2.setColor(Color.BLACK); g2.setFont(paramFont); g2.drawString(param + ":", padding + x, yy); g2.setFont(valueFont); g2.drawString(value, width - padding - valueFontMetrics.stringWidth(value) + x, yy); // Same as yy += valueFontMetrics.getDescent() + paramValueHeightMargin + valueFontMetrics.getAscent() yy += paramValueHeightMargin + Math.max(paramFontMetrics.getHeight(), valueFontMetrics.getHeight()); } if (values.size() > 1) { yy -= paramValueHeightMargin; yy += infoTotalHeightMargin; if (totalValue > 0.0) { g2.setColor(JStockOptions.DEFAULT_HIGHER_NUMERICAL_VALUE_FOREGROUND_COLOR); } else if (totalValue < 0.0) { g2.setColor(JStockOptions.DEFAULT_LOWER_NUMERICAL_VALUE_FOREGROUND_COLOR); } g2.setFont(paramFont); g2.drawString(totalParam + ":", padding + x, yy); g2.setFont(valueFont); final DecimalPlace decimalPlace = JStock.instance().getJStockOptions().getDecimalPlace(); final String totalValueStr = org.yccheok.jstock.portfolio.Utils.toCurrencyWithSymbol(decimalPlace, totalValue); g2.drawString(totalValueStr, width - padding - valueFontMetrics.stringWidth(totalValueStr) + x, yy); } g2.setColor(oldColor); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldValueAntiAlias); g2.setFont(oldFont); }
From source file:org.yccheok.jstock.gui.charting.ChartLayerUI.java
private void drawInformationBox(Graphics2D g2, JXLayer<? extends V> layer) { if (JStock.instance().getJStockOptions() .getYellowInformationBoxOption() == JStockOptions.YellowInformationBoxOption.Hide) { return;//from w ww .ja va 2s . c o m } final Font oldFont = g2.getFont(); final Font paramFont = oldFont; final FontMetrics paramFontMetrics = g2.getFontMetrics(paramFont); final Font valueFont = oldFont.deriveFont(oldFont.getStyle() | Font.BOLD, (float) oldFont.getSize() + 1); final FontMetrics valueFontMetrics = g2.getFontMetrics(valueFont); final Font dateFont = oldFont.deriveFont((float) oldFont.getSize() - 1); final FontMetrics dateFontMetrics = g2.getFontMetrics(dateFont); final List<ChartData> chartDatas = this.chartJDialog.getChartDatas(); List<String> values = new ArrayList<String>(); final ChartData chartData = chartDatas.get(this.mainTraceInfo.getDataIndex()); // Number formats are generally not synchronized. It is recommended to create separate format instances for each thread. // If multiple threads access a format concurrently, it must be synchronized externally. // http://stackoverflow.com/questions/2213410/usage-of-decimalformat-for-the-following-case final DecimalFormat integerFormat = new DecimalFormat("###,###"); // It is common to use OHLC for chat, instead of using PrevPrice. values.add(org.yccheok.jstock.gui.Utils.stockPriceDecimalFormat(chartData.openPrice)); values.add(org.yccheok.jstock.gui.Utils.stockPriceDecimalFormat(chartData.highPrice)); values.add(org.yccheok.jstock.gui.Utils.stockPriceDecimalFormat(chartData.lowPrice)); values.add(org.yccheok.jstock.gui.Utils.stockPriceDecimalFormat(chartData.lastPrice)); values.add(integerFormat.format(chartData.volume)); final List<String> indicatorParams = new ArrayList<String>(); final List<String> indicatorValues = new ArrayList<String>(); final DecimalFormat decimalFormat = new DecimalFormat("0.00"); for (TraceInfo indicatorTraceInfo : this.indicatorTraceInfos) { final int plotIndex = indicatorTraceInfo.getPlotIndex(); final int seriesIndex = indicatorTraceInfo.getSeriesIndex(); final int dataIndex = indicatorTraceInfo.getDataIndex(); final String name = this.getLegendName(plotIndex, seriesIndex); final Number value = this.getValue(plotIndex, seriesIndex, dataIndex); if (name == null || value == null) { continue; } indicatorParams.add(name); indicatorValues.add(decimalFormat.format(value)); } assert (values.size() == params.size()); int index = 0; final int paramValueWidthMargin = 10; final int paramValueHeightMargin = 0; // Slightly larger than dateInfoHeightMargin, as font for indicator is // larger than date's. final int infoIndicatorHeightMargin = 8; int maxInfoWidth = -1; // paramFontMetrics will always "smaller" than valueFontMetrics. int totalInfoHeight = Math.max(paramFontMetrics.getHeight(), valueFontMetrics.getHeight()) * values.size() + paramValueHeightMargin * (values.size() - 1); for (String param : params) { final String value = values.get(index++); final int paramStringWidth = paramFontMetrics.stringWidth(param + ":") + paramValueWidthMargin + valueFontMetrics.stringWidth(value); if (maxInfoWidth < paramStringWidth) { maxInfoWidth = paramStringWidth; } } if (indicatorValues.size() > 0) { totalInfoHeight += infoIndicatorHeightMargin; totalInfoHeight += Math.max(paramFontMetrics.getHeight(), valueFontMetrics.getHeight()) * indicatorValues.size() + paramValueHeightMargin * (indicatorValues.size() - 1); index = 0; for (String indicatorParam : indicatorParams) { final String indicatorValue = indicatorValues.get(index++); final int paramStringWidth = paramFontMetrics.stringWidth(indicatorParam + ":") + paramValueWidthMargin + valueFontMetrics.stringWidth(indicatorValue); if (maxInfoWidth < paramStringWidth) { maxInfoWidth = paramStringWidth; } } } final Date date = new Date(chartData.timestamp); // Date formats are not synchronized. It is recommended to create separate format instances for each thread. // If multiple threads access a format concurrently, it must be synchronized externally. final SimpleDateFormat simpleDateFormat = this.simpleDataFormatThreadLocal.get(); final String dateString = simpleDateFormat.format(date); final int dateStringWidth = dateFontMetrics.stringWidth(dateString); final int dateStringHeight = dateFontMetrics.getHeight(); // We want to avoid information box from keep changing its width while // user moves along the mouse. This will prevent user from feeling, // information box is flickering, which is uncomfortable to user's eye. final int maxStringWidth = Math.max(dateFontMetrics.stringWidth(longDateString), Math.max(this.maxWidth, Math.max(dateStringWidth, maxInfoWidth))); if (maxStringWidth > this.maxWidth) { this.maxWidth = maxStringWidth; } final int dateInfoHeightMargin = 5; final int maxStringHeight = dateStringHeight + dateInfoHeightMargin + totalInfoHeight; final int padding = 5; final int boxPointMargin = 8; final int width = maxStringWidth + (padding << 1); final int height = maxStringHeight + (padding << 1); /* Get Border Rect Information. */ /* fillRect(1, 1, 1, 1); // O is rect pixel xxx xOx xxx drawRect(0, 0, 2, 2); // O is rect pixel OOO OxO OOO */ final int borderWidth = width + 2; final int borderHeight = height + 2; // On left side of the ball. final double suggestedBorderX = this.mainTraceInfo.getPoint().getX() - borderWidth - boxPointMargin; final double suggestedBorderY = this.mainTraceInfo.getPoint().getY() - (borderHeight >> 1); double bestBorderX = 0; double bestBorderY = 0; if (JStock.instance().getJStockOptions() .getYellowInformationBoxOption() == JStockOptions.YellowInformationBoxOption.Stay) { if (this.mainTraceInfo.getPoint() .getX() > ((int) (this.mainDrawArea.getX() + this.mainDrawArea.getWidth() + 0.5) >> 1)) { bestBorderX = this.mainDrawArea.getX(); bestBorderY = this.mainDrawArea.getY(); } else { bestBorderX = this.mainDrawArea.getX() + this.mainDrawArea.getWidth() - borderWidth; bestBorderY = this.mainDrawArea.getY(); } } else { assert (JStock.instance().getJStockOptions() .getYellowInformationBoxOption() == JStockOptions.YellowInformationBoxOption.Follow); bestBorderX = suggestedBorderX > this.mainDrawArea.getX() ? (suggestedBorderX + borderWidth) < (this.mainDrawArea.getX() + this.mainDrawArea.getWidth()) ? suggestedBorderX : this.mainDrawArea.getX() + this.mainDrawArea.getWidth() - borderWidth - boxPointMargin : this.mainTraceInfo.getPoint().getX() + boxPointMargin; bestBorderY = suggestedBorderY > this.mainDrawArea.getY() ? (suggestedBorderY + borderHeight) < (this.mainDrawArea.getY() + this.mainDrawArea.getHeight()) ? suggestedBorderY : this.mainDrawArea.getY() + this.mainDrawArea.getHeight() - borderHeight - boxPointMargin : this.mainDrawArea.getY() + boxPointMargin; } final double x = bestBorderX + 1; final double y = bestBorderY + 1; final Object oldValueAntiAlias = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING); final Composite oldComposite = g2.getComposite(); final Color oldColor = g2.getColor(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(COLOR_BORDER); g2.drawRoundRect((int) (bestBorderX + 0.5), (int) (bestBorderY + 0.5), borderWidth - 1, borderHeight - 1, 15, 15); g2.setColor(COLOR_BACKGROUND); g2.setComposite(Utils.makeComposite(0.75f)); g2.fillRoundRect((int) (x + 0.5), (int) (y + 0.5), width, height, 15, 15); g2.setComposite(oldComposite); g2.setColor(oldColor); int yy = (int) (y + padding + dateFontMetrics.getAscent() + 0.5); g2.setFont(dateFont); g2.setColor(COLOR_BLUE); g2.drawString(dateString, (int) (((width - dateFontMetrics.stringWidth(dateString)) >> 1) + x + 0.5), yy); index = 0; yy += dateFontMetrics.getDescent() + dateInfoHeightMargin + valueFontMetrics.getAscent(); final String CLOSE_STR = GUIBundle.getString("StockHistory_Close"); for (String param : params) { final String value = values.get(index++); g2.setColor(Color.BLACK); if (param.equals(CLOSE_STR)) { // It is common to use OHLC for chat, instead of using PrevPrice. final double changePrice = chartData.lastPrice - chartData.openPrice; if (changePrice > 0.0) { g2.setColor(JStockOptions.DEFAULT_HIGHER_NUMERICAL_VALUE_FOREGROUND_COLOR); } else if (changePrice < 0.0) { g2.setColor(JStockOptions.DEFAULT_LOWER_NUMERICAL_VALUE_FOREGROUND_COLOR); } } g2.setFont(paramFont); g2.drawString(param + ":", (int) (padding + x + 0.5), yy); g2.setFont(valueFont); g2.drawString(value, (int) (width - padding - valueFontMetrics.stringWidth(value) + x + 0.5), yy); // Same as yy += valueFontMetrics.getDescent() + paramValueHeightMargin + valueFontMetrics.getAscent() yy += paramValueHeightMargin + valueFontMetrics.getHeight(); } g2.setColor(Color.BLACK); yy -= paramValueHeightMargin; yy += infoIndicatorHeightMargin; index = 0; for (String indicatorParam : indicatorParams) { final String indicatorValue = indicatorValues.get(index++); g2.setFont(paramFont); g2.drawString(indicatorParam + ":", (int) (padding + x + 0.5), yy); g2.setFont(valueFont); g2.drawString(indicatorValue, (int) (width - padding - valueFontMetrics.stringWidth(indicatorValue) + x + 0.5), yy); // Same as yy += valueFontMetrics.getDescent() + paramValueHeightMargin + valueFontMetrics.getAscent() yy += paramValueHeightMargin + valueFontMetrics.getHeight(); } g2.setColor(oldColor); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldValueAntiAlias); g2.setFont(oldFont); }
From source file:org.yccheok.jstock.gui.charting.InvestmentFlowLayerUI.java
private void updateROIInformationBox(Graphics2D g2) { final Font oldFont = g2.getFont(); final Font paramFont = oldFont; final FontMetrics paramFontMetrics = g2.getFontMetrics(paramFont); final Font valueFont = oldFont.deriveFont(oldFont.getStyle() | Font.BOLD, (float) oldFont.getSize() + 1); final FontMetrics valueFontMetrics = g2.getFontMetrics(valueFont); final Font dateFont = oldFont.deriveFont((float) oldFont.getSize() - 1); final FontMetrics dateFontMetrics = g2.getFontMetrics(dateFont); final Activities activities = this.investmentFlowChartJDialog.getROIActivities(this.ROIPointIndex); this.ROIValues.clear(); this.ROIParams.clear(); this.totalROIValue = 0.0; final DecimalPlace decimalPlace = JStock.instance().getJStockOptions().getDecimalPlace(); for (int i = 0, size = activities.size(); i < size; i++) { final Activity activity = activities.get(i); // Buy, Sell or Dividend only. if (activity.getType() == Activity.Type.Buy) { final double quantity = (Double) activity.get(Activity.Param.Quantity); final StockInfo stockInfo = (StockInfo) activity.get(Activity.Param.StockInfo); this.ROIParams.add(GUIBundle.getString("InvestmentFlowLayerUI_Own") + " " + org.yccheok.jstock.portfolio.Utils.toQuantity(quantity) + " " + stockInfo.symbol); final double amount = convertToPoundIfNecessary(stockInfo.code, quantity * this.investmentFlowChartJDialog.getStockPrice(stockInfo.code)); this.totalROIValue += amount; this.ROIValues.add(org.yccheok.jstock.portfolio.Utils.toCurrencyWithSymbol(decimalPlace, amount)); } else if (activity.getType() == Activity.Type.Sell) { final double quantity = (Double) activity.get(Activity.Param.Quantity); final StockInfo stockInfo = (StockInfo) activity.get(Activity.Param.StockInfo); this.ROIParams.add(activity.getType() + " " + org.yccheok.jstock.portfolio.Utils.toQuantity(quantity) + " " + stockInfo.symbol); final double amount = convertToPoundIfNecessary(stockInfo.code, activity.getAmount()); this.totalROIValue += amount; this.ROIValues.add(org.yccheok.jstock.portfolio.Utils.toCurrencyWithSymbol(decimalPlace, amount)); } else if (activity.getType() == Activity.Type.Dividend) { final StockInfo stockInfo = (StockInfo) activity.get(Activity.Param.StockInfo); this.ROIParams.add(activity.getType() + " " + stockInfo.symbol); final double amount = activity.getAmount(); this.totalROIValue += amount; this.ROIValues.add(org.yccheok.jstock.portfolio.Utils.toCurrencyWithSymbol(decimalPlace, amount)); } else {//from www .j av a2s. c o m assert (false); } } final boolean isTotalNeeded = this.ROIParams.size() > 1; final String totalParam = GUIBundle.getString("InvestmentFlowLayerUI_Total_Return"); final String totalValue = org.yccheok.jstock.portfolio.Utils.toCurrencyWithSymbol(decimalPlace, this.totalROIValue); /* This is the height for "total" information. */ int totalHeight = 0; assert (this.ROIParams.size() == this.ROIValues.size()); int index = 0; final int paramValueWidthMargin = 10; final int paramValueHeightMargin = 0; int maxInfoWidth = -1; // paramFontMetrics will always "smaller" than valueFontMetrics. int totalInfoHeight = Math.max(paramFontMetrics.getHeight(), valueFontMetrics.getHeight()) * this.ROIValues.size() + paramValueHeightMargin * (this.ROIValues.size() - 1); for (String param : this.ROIParams) { final String value = this.ROIValues.get(index++); final int paramStringWidth = paramFontMetrics.stringWidth(param + ":") + paramValueWidthMargin + valueFontMetrics.stringWidth(value); if (maxInfoWidth < paramStringWidth) { maxInfoWidth = paramStringWidth; } } if (isTotalNeeded) { final int tmp = paramFontMetrics.stringWidth(totalParam + ":") + paramValueWidthMargin + valueFontMetrics.stringWidth(totalValue); if (maxInfoWidth < tmp) { maxInfoWidth = tmp; } totalHeight = Math.max(paramFontMetrics.getHeight(), valueFontMetrics.getHeight()); } final Date date = activities.getDate().getTime(); final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE, MMMM d, yyyy"); final String dateString = simpleDateFormat.format(date); final int dateStringWidth = dateFontMetrics.stringWidth(dateString); final int dateStringHeight = dateFontMetrics.getHeight(); final int maxStringWidth = Math.max(dateStringWidth, maxInfoWidth); final int dateInfoHeightMargin = 5; final int infoTotalHeightMargin = 5; final int maxStringHeight = isTotalNeeded ? (dateStringHeight + dateInfoHeightMargin + totalInfoHeight + infoTotalHeightMargin + totalHeight) : (dateStringHeight + dateInfoHeightMargin + totalInfoHeight); // Now, We have a pretty good information on maxStringWidth and maxStringHeight. final int padding = 5; final int boxPointMargin = 8; final int width = maxStringWidth + (padding << 1); final int height = maxStringHeight + (padding << 1); final int borderWidth = width + 2; final int borderHeight = height + 2; // On left side of the ball. final double suggestedBorderX = this.ROIPoint.getX() - borderWidth - boxPointMargin; final double suggestedBorderY = this.ROIPoint.getY() - (borderHeight >> 1); final double bestBorderX = suggestedBorderX > this.drawArea.getX() ? (suggestedBorderX + borderWidth) < (this.drawArea.getX() + this.drawArea.getWidth()) ? suggestedBorderX : this.drawArea.getX() + this.drawArea.getWidth() - borderWidth - boxPointMargin : this.ROIPoint.getX() + boxPointMargin; final double bestBorderY = suggestedBorderY > this.drawArea.getY() ? (suggestedBorderY + borderHeight) < (this.drawArea.getY() + this.drawArea.getHeight()) ? suggestedBorderY : this.drawArea.getY() + this.drawArea.getHeight() - borderHeight - boxPointMargin : this.drawArea.getY() + boxPointMargin; final double x = bestBorderX + 1; final double y = bestBorderY + 1; this.ROIRect.setRect(x, y, width, height); }