OSDN Git Service

パッケージの整理を開始。文字フォントを塗りつぶすように変更すうr。
[gokigen/MeMoMa.git] / app / src / main / java / jp / sourceforge / gokigen / memoma / SelectLineShapeDialog.java
1 package jp.sourceforge.gokigen.memoma;
2
3 import android.app.AlertDialog;
4 import android.app.Dialog;
5 import android.content.Context;
6 import android.content.DialogInterface;
7 import android.graphics.Color;
8 import android.graphics.PorterDuff.Mode;
9 import android.graphics.drawable.BitmapDrawable;
10 import android.util.Log;
11 import android.view.LayoutInflater;
12 import android.view.View;
13 import android.widget.ImageButton;
14
15 import jp.sourceforge.gokigen.memoma.holders.LineStyleHolder;
16
17 /**
18  *   接続線の形状を選択するダイアログを表示する
19  * 
20  * @author MRSa
21  *
22  */
23 public class SelectLineShapeDialog implements ImageButton.OnClickListener
24 {
25         private int lineThickness = LineStyleHolder.LINETHICKNESS_THIN;
26         private int lineStyle = LineStyleHolder.LINESTYLE_STRAIGHT_NO_ARROW;
27         private int lineShape = LineStyleHolder.LINESHAPE_NORMAL;
28         
29         private Context context = null; 
30         private IResultReceiver resultReceiver = null;
31         private LineStyleHolder lineStyleHolder = null;
32         
33         private View dialogLayout = null;
34
35         /**
36          *    コンストラクタ
37          * 
38          * @param arg
39          * @param holder
40          */
41         public SelectLineShapeDialog(Context arg, LineStyleHolder holder)
42         {
43                 context = arg;
44                 lineStyleHolder = holder;
45         }
46
47         /**
48          *    ダイアログで選択した結果を受信するためのレシーバを設定する
49          * 
50          * @param receiver
51          */
52         public void setResultReceiver(IResultReceiver receiver)
53         {
54                 resultReceiver = receiver;
55         }
56         
57     /**
58      *   確認ダイアログを応答する
59      * @return
60      */
61     public Dialog getDialog()
62     {
63         LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
64         final View layout = inflater.inflate(R.layout.lineselection, null);
65         dialogLayout = layout;
66
67         AlertDialog.Builder builder = new AlertDialog.Builder(context);
68
69         //  ダイアログで表示するデータを設定する場所
70
71         // 現在の線の形状と種類を取得する
72         lineShape = lineStyleHolder.getLineShape();
73         lineStyle = lineStyleHolder.getLineStyle();
74         lineThickness = lineStyleHolder.getLineThickness();
75
76         // 線の太さ
77         final ImageButton thin = (ImageButton) layout.findViewById(R.id.btnLineThicknessThin);
78         thin.setOnClickListener(this);
79         final ImageButton middle = (ImageButton) layout.findViewById(R.id.btnLineThicknessMiddle);
80         middle.setOnClickListener(this);
81         final ImageButton heavy = (ImageButton) layout.findViewById(R.id.btnLineThicknessHeavy);
82         heavy.setOnClickListener(this);
83
84         // 線の形状
85         final ImageButton straight = (ImageButton) layout.findViewById(R.id.btnLineShapeStraight);
86         straight.setOnClickListener(this);
87         final ImageButton tree = (ImageButton) layout.findViewById(R.id.btnLineShapeTree);
88         tree.setOnClickListener(this);
89         final ImageButton curve = (ImageButton) layout.findViewById(R.id.btnLineShapeCurve);
90         curve.setOnClickListener(this);
91         final ImageButton straightDash = (ImageButton) layout.findViewById(R.id.btnLineShapeStraightDash);
92         straightDash.setOnClickListener(this);
93         final ImageButton treeDash = (ImageButton) layout.findViewById(R.id.btnLineShapeTreeDash);
94         treeDash.setOnClickListener(this);
95         final ImageButton curveDash = (ImageButton) layout.findViewById(R.id.btnLineShapeCurveDash);
96         curveDash.setOnClickListener(this);
97         final ImageButton straightRarrow = (ImageButton) layout.findViewById(R.id.btnLineShapeStraightRarrow);
98         straightRarrow.setOnClickListener(this);
99         final ImageButton treeRarrow = (ImageButton) layout.findViewById(R.id.btnLineShapeTreeRarrow);
100         treeRarrow.setOnClickListener(this);
101         final ImageButton curveRarrow = (ImageButton) layout.findViewById(R.id.btnLineShapeCurveRarrow);
102         curveRarrow.setOnClickListener(this);
103         final ImageButton straightRarrowDash = (ImageButton) layout.findViewById(R.id.btnLineShapeStraightRarrowDash);
104         straightRarrowDash.setOnClickListener(this);
105         final ImageButton treeRarrowDash = (ImageButton) layout.findViewById(R.id.btnLineShapeTreeRarrowDash);
106         treeRarrowDash.setOnClickListener(this);
107         final ImageButton curveRarrowDash = (ImageButton) layout.findViewById(R.id.btnLineShapeCurveRarrowDash);
108         curveRarrowDash.setOnClickListener(this);
109
110         builder.setView(layout);
111         builder.setTitle(context.getString(R.string.Title_SelectLineShape));
112         builder.setCancelable(false);
113         builder.setPositiveButton(context.getString(R.string.confirmYes), new DialogInterface.OnClickListener()
114               {
115                    public void onClick(DialogInterface dialog, int id)
116                    {
117                            boolean ret = false;
118                            setLineShape(lineStyle, lineShape, lineThickness);
119                            if (resultReceiver != null)
120                            {
121                                resultReceiver.finishSelectLineShape(lineStyle, lineShape, lineThickness);
122                            }
123                            updateButtonHighlightLineThickness(0);
124                            updateButtonHighlightLineShape(0);
125                        if (ret == true)
126                        {
127                            dialog.dismiss();
128                        }
129                        else
130                        {
131                            dialog.cancel();
132                        }
133                        System.gc();
134                    }
135                });
136         builder.setNegativeButton(context.getString(R.string.confirmNo), new DialogInterface.OnClickListener()
137                {
138                    public void onClick(DialogInterface dialog, int id)
139                    {
140                            boolean ret = false;
141                            if (resultReceiver != null)
142                            {
143                                resultReceiver.cancelSelectLineShape();
144                            }
145                            updateButtonHighlightLineThickness(0);
146                            updateButtonHighlightLineShape(0);
147                        if (ret == true)
148                        {
149                            dialog.dismiss();
150                        }
151                        else
152                        {
153                            dialog.cancel();
154                        }
155                        System.gc();
156                    }
157                });
158         return (builder.create());      
159     }
160
161     /**
162      *    オブジェクト入力用ダイアログの表示を準備する
163      *    (ダイアログの表示をした時に呼ばれる)
164      */
165     public void prepareSelectLineShapeDialog(Dialog dialog, Integer objectKey)
166     {
167         // 現在の線の形状と種類を取得する
168         lineShape = lineStyleHolder.getLineShape();
169         lineStyle = lineStyleHolder.getLineStyle();
170         lineThickness = lineStyleHolder.getLineThickness();
171
172         // 画面(ダイアログ)で、現在選択中のものをハイライトにする。
173         updateButtonHighlightLineThickness(getLineThicknessButtonId(lineThickness));
174         updateButtonHighlightLineShape(getLineShapeButtonId(lineStyle, lineShape));
175
176     }
177
178     /**
179      *    線の形状を設定する
180      * 
181      * @param toSetLineStyle
182      * @param toSetLineShape
183      */
184     public void setLineShape(int toSetLineStyle, int toSetLineShape, int toSetLineThickness)
185     {
186         lineStyleHolder.setLineShape(toSetLineShape);
187         lineStyleHolder.setLineStyle(toSetLineStyle);
188         lineStyleHolder.setLineThickness(toSetLineThickness);
189         
190         Log.v(Main.APP_IDENTIFIER, ":::CHANGE LINE :::  shape:" + toSetLineShape + " style:" + toSetLineStyle + " thickness:" + toSetLineThickness);
191     }
192
193     /**
194      * 
195      * 
196      * @param id       更新するボタンのID
197      * @param judge 判断するボタンのID
198      */
199     private void setButtonBorder(int id, int judge)
200     {
201         try
202         {
203             ImageButton button = (ImageButton) dialogLayout.findViewById(id);
204             //GradientDrawable btnBackgroundShape = (GradientDrawable)button.getBackground();
205             BitmapDrawable btnBackgroundShape = (BitmapDrawable)button.getBackground();
206             if (id == judge)
207             {
208                 //btnBackgroundShape.setColorFilter(Color.rgb(51, 181, 229), Mode.LIGHTEN);
209                 btnBackgroundShape.setColorFilter(Color.BLUE, Mode.LIGHTEN);
210             }
211             else
212             {
213                 btnBackgroundShape.setColorFilter(Color.BLACK, Mode.LIGHTEN);
214             } 
215         }
216         catch (Exception ex)
217         {
218                 // 
219                 Log.v(Main.APP_IDENTIFIER, "setButtonBorder(): " + ex.toString());
220         }
221         
222     }
223     
224     /**
225      *    イメージボタンの選択状態を更新する (接続線の太さ)
226      * 
227      * @param buttonId
228      */
229     private void updateLineThickness(int buttonId)
230     {
231         switch (buttonId)
232         {
233           case R.id.btnLineThicknessMiddle:
234                 lineThickness = LineStyleHolder.LINETHICKNESS_MIDDLE;
235                 break;
236           case R.id.btnLineThicknessHeavy:
237                 lineThickness = LineStyleHolder.LINETHICKNESS_HEAVY;
238                 break;
239           case R.id.btnLineThicknessThin:
240           default:
241                 lineThickness = LineStyleHolder.LINETHICKNESS_THIN;
242             break;
243         }
244     }
245
246     /**
247      *    線の形状の選択状態を記憶(更新)する
248      * 
249      * @param buttonId
250      */
251     private void updateLineStyle(int buttonId)
252     {
253         switch (buttonId)
254         {
255           case R.id.btnLineShapeTree:
256                 lineStyle = LineStyleHolder.LINESTYLE_TREESTYLE_NO_ARROW;
257                 lineShape = LineStyleHolder.LINESHAPE_NORMAL;
258                 break;
259
260           case R.id.btnLineShapeCurve:
261                 lineStyle = LineStyleHolder.LINESTYLE_CURVESTYLE_NO_ARROW;
262                 lineShape = LineStyleHolder.LINESHAPE_NORMAL;
263                 break;
264
265           case R.id.btnLineShapeStraightDash:
266                 lineStyle = LineStyleHolder.LINESTYLE_STRAIGHT_NO_ARROW;
267                 lineShape = LineStyleHolder.LINESHAPE_DASH;
268             break;
269
270           case R.id.btnLineShapeTreeDash:
271                 lineStyle = LineStyleHolder.LINESTYLE_TREESTYLE_NO_ARROW;
272                 lineShape = LineStyleHolder.LINESHAPE_DASH;
273             break;
274
275           case R.id.btnLineShapeCurveDash:
276                 lineStyle = LineStyleHolder.LINESTYLE_CURVESTYLE_NO_ARROW;
277                 lineShape = LineStyleHolder.LINESHAPE_DASH;
278                 break;
279
280           case R.id.btnLineShapeStraightRarrow:
281                 lineStyle = LineStyleHolder.LINESTYLE_STRAIGHT_R_ARROW;
282                 lineShape = LineStyleHolder.LINESHAPE_NORMAL;
283             break;
284
285           case R.id.btnLineShapeTreeRarrow:
286             lineStyle = LineStyleHolder.LINESTYLE_TREESTYLE_R_ARROW;
287             lineShape = LineStyleHolder.LINESHAPE_NORMAL;
288             break;
289
290           case R.id.btnLineShapeCurveRarrow:
291                 lineStyle = LineStyleHolder.LINESTYLE_CURVESTYLE_R_ARROW;
292                 lineShape = LineStyleHolder.LINESHAPE_NORMAL;
293             break;
294                 
295           case R.id.btnLineShapeStraightRarrowDash:
296                 lineStyle = LineStyleHolder.LINESTYLE_STRAIGHT_R_ARROW;
297                 lineShape = LineStyleHolder.LINESHAPE_DASH;
298             break;
299
300           case R.id.btnLineShapeTreeRarrowDash:
301                 lineStyle = LineStyleHolder.LINESTYLE_TREESTYLE_R_ARROW;
302                 lineShape = LineStyleHolder.LINESHAPE_DASH;
303             break;
304
305           case R.id.btnLineShapeCurveRarrowDash:
306                 lineStyle = LineStyleHolder.LINESTYLE_CURVESTYLE_R_ARROW;
307                 lineShape = LineStyleHolder.LINESHAPE_DASH;
308             break;
309
310           case R.id.btnLineShapeStraight:
311           default:
312                 lineStyle = LineStyleHolder.LINESTYLE_STRAIGHT_NO_ARROW;
313                 lineShape = LineStyleHolder.LINESHAPE_NORMAL;
314             break;
315         }
316         
317     }    
318     /**
319      *    現在の太さを設定する
320      * 
321      * @param thickness
322      * @return
323      */
324     private int getLineThicknessButtonId(int thickness)
325     {
326         int buttonId = R.id.btnLineThicknessThin;
327         switch (thickness)
328         {
329               case LineStyleHolder.LINETHICKNESS_HEAVY:
330             buttonId = R.id.btnLineThicknessHeavy;
331             break;
332
333           case LineStyleHolder.LINETHICKNESS_MIDDLE:
334               buttonId = R.id.btnLineThicknessMiddle;
335               break;
336
337           case LineStyleHolder.LINETHICKNESS_THIN:
338           default:
339                   buttonId = R.id.btnLineThicknessThin;
340                   break;
341         }
342         return (buttonId);
343     }    
344     
345     /**
346      * 
347      * 
348      * @param currentLineStyle
349      * @param currentLineShape
350      * @return
351      */
352     private int getLineShapeButtonId(int currentLineStyle, int currentLineShape)
353     {
354         int buttonId = R.id.btnLineShapeStraight;
355         
356         if ((currentLineStyle == LineStyleHolder.LINESTYLE_TREESTYLE_NO_ARROW)&&
357                         (currentLineShape == LineStyleHolder.LINESHAPE_NORMAL))
358         {
359                 buttonId = R.id.btnLineShapeTree;
360         }
361         else if ((currentLineStyle == LineStyleHolder.LINESTYLE_CURVESTYLE_NO_ARROW)&&
362                         (currentLineShape == LineStyleHolder.LINESHAPE_NORMAL))
363         {
364                 buttonId = R.id.btnLineShapeCurve;
365         }
366         else if ((currentLineStyle == LineStyleHolder.LINESTYLE_STRAIGHT_NO_ARROW)&&
367                         (currentLineShape == LineStyleHolder.LINESHAPE_DASH))
368         {
369                 buttonId = R.id.btnLineShapeStraightDash;
370         }
371         else if ((currentLineStyle == LineStyleHolder.LINESTYLE_TREESTYLE_NO_ARROW)&&
372                         (currentLineShape == LineStyleHolder.LINESHAPE_DASH))
373         {
374                 buttonId = R.id.btnLineShapeTreeDash;
375         }
376         else if ((currentLineStyle == LineStyleHolder.LINESTYLE_CURVESTYLE_NO_ARROW)&&
377                         (currentLineShape == LineStyleHolder.LINESHAPE_DASH))
378         {
379                 buttonId = R.id.btnLineShapeCurveDash;
380         }
381         else if ((currentLineStyle == LineStyleHolder.LINESTYLE_STRAIGHT_R_ARROW)&&
382                         (currentLineShape == LineStyleHolder.LINESHAPE_NORMAL))
383         {
384                 buttonId = R.id.btnLineShapeStraightRarrow;
385         }
386         else if ((currentLineStyle == LineStyleHolder.LINESTYLE_TREESTYLE_R_ARROW)&&
387                         (currentLineShape == LineStyleHolder.LINESHAPE_NORMAL))
388         {
389                 buttonId = R.id.btnLineShapeTreeRarrow;
390         }
391         else if ((currentLineStyle == LineStyleHolder.LINESTYLE_CURVESTYLE_R_ARROW)&&
392                         (currentLineShape == LineStyleHolder.LINESHAPE_NORMAL))
393         {
394                 buttonId = R.id.btnLineShapeCurveRarrow;
395         }
396         else if ((currentLineStyle == LineStyleHolder.LINESTYLE_STRAIGHT_R_ARROW)&&
397                         (currentLineShape == LineStyleHolder.LINESHAPE_DASH))
398         {
399                 buttonId = R.id.btnLineShapeStraightRarrowDash;
400         }
401         else if ((currentLineStyle == LineStyleHolder.LINESTYLE_TREESTYLE_R_ARROW)&&
402                         (currentLineShape == LineStyleHolder.LINESHAPE_DASH))
403         {
404                 buttonId = R.id.btnLineShapeTreeRarrowDash;
405         }
406         else if ((currentLineStyle == LineStyleHolder.LINESTYLE_CURVESTYLE_R_ARROW)&&
407                         (currentLineShape == LineStyleHolder.LINESHAPE_DASH))
408         {
409                 buttonId = R.id.btnLineShapeCurveRarrowDash;
410         }
411         /**
412         else  if ((currentLineStyle == LineStyleHolder.LINESTYLE_STRAIGHT_NO_ARROW)&&
413                         (currentLineShape == LineStyleHolder.LINESHAPE_NORMAL))
414         {
415                 buttonId = R.id.btnLineShapeStraight;
416         }
417         **/
418         return (buttonId);
419     }
420
421     /**
422      * 
423      *     
424      * @param id
425      */
426     private void updateButtonHighlightLineThickness(int id)
427     {
428         setButtonBorder(R.id.btnLineThicknessThin, id); 
429         setButtonBorder(R.id.btnLineThicknessMiddle, id);       
430         setButtonBorder(R.id.btnLineThicknessHeavy, id);        
431     }
432     
433     /**
434      * 
435      * 
436      * @param id
437      */
438     private void updateButtonHighlightLineShape(int id)
439     {
440         setButtonBorder(R.id.btnLineShapeStraight, id); 
441         setButtonBorder(R.id.btnLineShapeTree, id);     
442         setButtonBorder(R.id.btnLineShapeCurve, id);    
443
444         setButtonBorder(R.id.btnLineShapeStraightDash, id);     
445         setButtonBorder(R.id.btnLineShapeTreeDash, id); 
446         setButtonBorder(R.id.btnLineShapeCurveDash, id);        
447
448         setButtonBorder(R.id.btnLineShapeStraightRarrow, id);   
449         setButtonBorder(R.id.btnLineShapeTreeRarrow, id);       
450         setButtonBorder(R.id.btnLineShapeCurveRarrow, id);      
451
452         setButtonBorder(R.id.btnLineShapeStraightRarrowDash, id);       
453         setButtonBorder(R.id.btnLineShapeTreeRarrowDash, id);   
454         setButtonBorder(R.id.btnLineShapeCurveRarrowDash, id);          
455     }
456     
457     /**
458      *    ボタンが押された時の処理...
459      * 
460      */
461     public void onClick(View v)
462     {
463         int id = v.getId();
464         
465         // 押されたボタンが接続線の太さだった場合...
466         if ((id == R.id.btnLineThicknessThin)||(id == R.id.btnLineThicknessMiddle)||(id == R.id.btnLineThicknessHeavy))
467         {
468                 updateButtonHighlightLineThickness(id);
469                 updateLineThickness(id);
470                 return;
471         }
472
473         // 線の形状を更新した場合...
474         updateButtonHighlightLineShape(id);
475         updateLineStyle(id);
476     }
477
478     public interface IResultReceiver
479     {
480         public abstract void finishSelectLineShape(int style, int shape, int thickness);
481         public abstract void cancelSelectLineShape();
482     }
483 }