Java tutorial
/* Copyright (C) 2013 NTT DATA Corporation This program is free software; you can redistribute it and/or Modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ package com.clustercontrol.calendar.composite; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.eclipse.draw2d.ColorConstantsWrapper; import org.eclipse.draw2d.FigureCanvas; import org.eclipse.draw2d.Label; import org.eclipse.draw2d.Panel; import org.eclipse.draw2d.RectangleFigure; import org.eclipse.draw2d.XYLayout; import org.eclipse.draw2d.geometry.Dimension; import org.eclipse.draw2d.geometry.Point; import org.eclipse.draw2d.geometry.Rectangle; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; import com.clustercontrol.bean.DayOfWeekConstant; import com.clustercontrol.calendar.util.CalendarEndpointWrapper; import com.clustercontrol.util.HinemosMessage; import com.clustercontrol.util.Messages; import com.clustercontrol.util.TimezoneUtil; import com.clustercontrol.ws.calendar.CalendarDetailInfo; import com.clustercontrol.ws.calendar.CalendarNotFound_Exception; import com.clustercontrol.ws.calendar.InvalidRole_Exception; /** * []?<BR> * * @version 4.1.0 * @since 4.1.0 */ public class CalendarWeekComposite extends Composite { // private static Log m_log = LogFactory.getLog(CalendarWeekComposite.class); // ??? 720(11440??) private static final int MAX_BAR_LENGTH = 720; // ??? private static final int BAR_HEIGHT = 15; // private static final int LEFT_MARGIN = 200; // ???? private static final int BAR_SPAN = 40; //1 private static final long DAY24 = 24 * 60 * 60 * 1000; //??? private FigureCanvas m_canvas; //??? private Panel m_panel; //??ID private Label m_labelId = null; //??? private Label[] m_labelMatrix; //?? ScheduleBar[] scheduleBars = null; /** * * * @param parent * @param style * @since 2.0.0 */ public CalendarWeekComposite(Composite parent, int style) { super(parent, style); initialize(); } /** * ??<BR> * * @since 4.1.0 */ private void initialize() { // ??parent???? GridData gridData = new GridData(); gridData.horizontalAlignment = GridData.FILL; gridData.verticalAlignment = GridData.FILL; gridData.grabExcessHorizontalSpace = true; gridData.grabExcessVerticalSpace = true; this.setLayoutData(gridData); // ??? this.setLayout(new FillLayout()); // ???? m_canvas = new FigureCanvas(this, SWT.DOUBLE_BUFFERED); // (bgimage????)? m_canvas.setBackground(ColorConstantsWrapper.white()); // ?? createPanel(); } /** * ??<BR> * * @since 4.1.0 */ private void createPanel() { if (m_panel != null) { m_panel.removeAll(); } //?? m_panel = new Panel(); m_canvas.setContents(m_panel); m_panel.setLayoutManager(new XYLayout()); //ID m_labelId = new Label(Messages.getString("calendar.id") + " : "); m_labelId.setVisible(true); m_panel.add(m_labelId); Dimension dimension = new Dimension(-1, -1); Point point = new Point(0, 0); Rectangle zeroRectangle = new Rectangle(point, dimension); m_panel.setConstraint(m_labelId, zeroRectangle); // for (int i = 0; i < 25; i += 3) { Label label = new Label(String.format("%02d:00", i)); label.setVisible(true); m_panel.add(label); dimension = new Dimension(-1, -1); point = new Point(30 * i + 183, 20); zeroRectangle = new Rectangle(point, dimension); m_panel.setConstraint(label, zeroRectangle); } // m_labelMatrix = new Label[7]; for (int i = 0; i < m_labelMatrix.length; i++) { m_labelMatrix[i] = new Label("----/--/--"); m_labelMatrix[i].setVisible(true); m_panel.add(m_labelMatrix[i]); dimension = new Dimension(-1, -1); point = new Point(30, (i + 1) * BAR_SPAN); zeroRectangle = new Rectangle(point, dimension); m_panel.setConstraint(m_labelMatrix[i], zeroRectangle); } } /** * ?<BR> * * @since 4.1.0 */ public void update(String managerName, String calendarId, int year, int month, int day) { if (calendarId == null) { return; } // ??? createPanel(); int period = 7; //?? Calendar now = Calendar.getInstance(TimezoneUtil.getTimeZone()); now.set(year, month - 1, day); int dayOfWeek = now.get(Calendar.DAY_OF_WEEK); m_panel.repaint(); // Sometimes labels disappear in RAP. Repaint to make sure labels show it //?(???) scheduleBars = createScheduleBars(m_panel, dayOfWeek); //??? for (ScheduleBar bar : scheduleBars) { bar.getInitBar().setBackgroundColor(ColorConstantsWrapper.red()); } try { for (int i = 0; i < period; i++) { //????? CalendarEndpointWrapper wrapper = CalendarEndpointWrapper.getWrapper(managerName); List<CalendarDetailInfo> detailList = wrapper.getCalendarWeek(calendarId, year, month, day); m_log.trace("detailList.size=" + detailList.size() + ", " + year + "/" + month + "/" + day); //???????? ArrayList<CalendarDetailInfo> detailListOrder = new ArrayList<CalendarDetailInfo>(); while (detailList.size() > 0) { int size = detailList.size(); detailListOrder.add(detailList.get(size - 1)); detailList.remove(size - 1); } for (CalendarDetailInfo detail : detailListOrder) { addScheduleBar(m_panel, dayOfWeek, detail.getTimeFrom(), detail.getTimeTo(), detail.isOperateFlg()); } // m_labelMatrix[i].setText(String.format("%02d/%02d/%02d", year, month, day) + " ( " + DayOfWeekConstant.typeToString(dayOfWeek) + " )"); //?? if (day + 1 > now.getActualMaximum(Calendar.DAY_OF_MONTH)) { //?? if (month == 12) { year++; month = 1; } else { month++; } day = 1; } else { day++; } //??? now.set(year, month - 1, day); dayOfWeek = now.get(Calendar.DAY_OF_WEEK); } } catch (InvalidRole_Exception e) { // ??? MessageDialog.openInformation(null, Messages.getString("message"), Messages.getString("message.accesscontrol.16")); } catch (CalendarNotFound_Exception e) { // ???????? m_log.info("update(), " + HinemosMessage.replace(e.getMessage())); } catch (Exception e) { // ? m_log.warn("update(), " + HinemosMessage.replace(e.getMessage()), e); MessageDialog.openError(null, Messages.getString("failed"), Messages.getString("message.hinemos.failure.unexpected") + ", " + HinemosMessage.replace(e.getMessage())); } //? if (calendarId.length() > 0) { m_labelId.setText(Messages.getString("calendar.id") + " : " + calendarId); } else { m_labelId.setText(Messages.getString("calendar.id") + " : "); } } /** * * ? : HourType??? * ?(day)????? * ???? * ????????????????? * @param panel * @param dayOfWeek * @param from * @param to */ private void addScheduleBar(Panel panel, int dayOfWeek, long from, long to, boolean operationFlg) { m_log.trace("dayofweek:" + dayOfWeek + ", from:" + from + ", to:" + to + ", operationflg:" + operationFlg); int timeZoneOffset = TimezoneUtil.getTimeZoneOffset(); int fromBar = (int) (MAX_BAR_LENGTH * ((float) (from + timeZoneOffset) / DAY24)); int toBar = (int) (MAX_BAR_LENGTH * ((float) (to + timeZoneOffset) / DAY24)); ScheduleBar scheduleBar = null; //???GUI?????????? for (ScheduleBar bar : scheduleBars) { if (bar.getDayOfWeek() == dayOfWeek) { scheduleBar = bar; } } //??????? RectangleFigure barToday = new RectangleFigure(); barToday.setOutline(false); if (operationFlg) { barToday.setBackgroundColor(ColorConstantsWrapper.green()); } else { barToday.setBackgroundColor(ColorConstantsWrapper.red()); } panel.add(barToday, new Rectangle(LEFT_MARGIN + fromBar, scheduleBar.getY(), toBar - fromBar, BAR_HEIGHT)); addCheckPoint(from, scheduleBar.getY(), false); addCheckPoint(to, scheduleBar.getY(), true); } /** * ? * ???? * @param key * @param y * @param endFlag * @param chkPointlbl */ private void addCheckPoint(long key, int y, boolean endFlag) { int timeZoneOffset = TimezoneUtil.getTimeZoneOffset(); long time = key + timeZoneOffset; if (time == 0 || time == DAY24) { return; } // SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); sdf.setTimeZone(TimezoneUtil.getTimeZone()); Label label = new Label(sdf.format(new Date(key))); label.setVisible(true); m_panel.add(label); Dimension dimension = new Dimension(-1, -1); int x = (int) (LEFT_MARGIN + time * MAX_BAR_LENGTH / DAY24); if (endFlag) { // x -= 55; // HH:mm:SS??????? x -= 35; } Point point = new Point(x, y); Rectangle rectangle = new Rectangle(point, dimension); m_panel.setConstraint(label, rectangle); } /** * GUI??????????? * ????????? * @param panel * @param dayOfWeek * @return */ private ScheduleBar[] createScheduleBars(Panel panel, int dayOfWeek) { ScheduleBar[] bars = new ScheduleBar[7]; for (int i = 0; i < bars.length; i++) { bars[i] = new ScheduleBar(dayOfWeek, LEFT_MARGIN, BAR_SPAN * (i + 1)); if (dayOfWeek == 7) { dayOfWeek = 1; } else { dayOfWeek++; } //????? bars[i].getInitBar().setBackgroundColor(ColorConstantsWrapper.white()); //???BAR_HEIGHT,MAX_BAR_LENGTH?? panel.add(bars[i].getInitBar(), new Rectangle(bars[i].getX(), bars[i].getY(), MAX_BAR_LENGTH, BAR_HEIGHT)); } return bars; } } /** * ?1?? * ????? * */ class ScheduleBar { //? private RectangleFigure initBar; //x private int x; //y private int y; // private int dayOfWeek; /** * * @param initBar * @param dayOfWeek * @param x * @param y */ public ScheduleBar(int dayOfWeek, int x, int y) { this.initBar = new RectangleFigure(); this.initBar.setOutline(false); this.dayOfWeek = dayOfWeek; this.x = x; this.y = y; } //? public RectangleFigure getInitBar() { return initBar; } //? public void setInitBar(RectangleFigure initBar) { this.initBar = initBar; } //x public int getX() { return x; } //x public void setX(int x) { this.x = x; } //y public int getY() { return y; } //y public void setY(int y) { this.y = y; } // public int getDayOfWeek() { return dayOfWeek; } // public void setDayOfWeek(int dayOfWeek) { this.dayOfWeek = dayOfWeek; } }