OSDN Git Service

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